简体   繁体   中英

How do I stop my character feeling like they're on ice using RigidBody2D Velocity

I am currently using RB2D velocity for movement but I can't make it stop sliding after I've finished pressing the button. It feels like the Ice level from Mario. How do I stop my character sliding everywhere?

My code is:

void Movement() {

     if (Input.GetAxisRaw ("Horizontal") > 0.1) {

         GetComponent<Rigidbody2D> ().velocity = new Vector2 (speed, GetComponent<Rigidbody2D> ().velocity.y);

     }
     if (Input.GetAxisRaw ("Horizontal") < -0.1) {

         GetComponent<Rigidbody2D> ().velocity = new Vector2 (-speed, GetComponent<Rigidbody2D> ().velocity.y);

     }
     if (Input.GetAxisRaw ("Vertical") > 0.5 && grounded) {

         GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, jumpHeight);

     }
}

My speed is set to 5, jumpHeight is 10 and I have both box collider and RigidBody2D on my player

Thanks for any help in advance

You can use the transform to move the object when pressing keys and directly move it without using the physics.

Also using the velocity, you can set it to 0.0f when you are not pressing any button to stop moving the object.

crabcrabcam is now using:

    void Movement() {

    if (Input.GetAxisRaw ("Horizontal") > 0.1) {

        GetComponent<Rigidbody2D> ().velocity = new Vector2 (speed, GetComponent<Rigidbody2D> ().velocity.y);

    } else if (Input.GetAxisRaw ("Horizontal") < -0.1) {

        GetComponent<Rigidbody2D> ().velocity = new Vector2 (-speed, GetComponent<Rigidbody2D> ().velocity.y);

    } else if (Input.GetAxisRaw("Horizontal") == 0 && grounded) {

        GetComponent<Rigidbody2D> ().velocity = new Vector2 (Vector2.zero, GetComponent<Rigidbody2D> ().velocity.y);

    }


    if (Input.GetAxisRaw ("Vertical") > 0.5 && grounded) {

        GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, jumpHeight);

    }

}

This makes it floaty in the air but whilst grounded it stops responsively :)

Add else if to the second if statement then another else statement you can use to stop moving the player.

if (Input.GetAxisRaw("Horizontal") > 0.1)
{
Move right
}
else if (Input.GetAxisRaw("Horizontal") < -0.1)
{
Move left
}
else
{
Stop moving/Vector2.zero;
}

Maybe something like this:

void Movement()
{

    if (Input.GetAxisRaw("Horizontal") > 0.1)
    {

        GetComponent<Rigidbody2D>().velocity = new Vector2(speed, GetComponent<Rigidbody2D>().velocity.y);

    }
    else if (Input.GetAxisRaw("Horizontal") < -0.1)
    {

        GetComponent<Rigidbody2D>().velocity = new Vector2(-speed, GetComponent<Rigidbody2D>().velocity.y);
    }
    else
    {
        GetComponent<Rigidbody2D>().velocity = Vector2.zero;
    }

    if (Input.GetAxisRaw("Vertical") > 0.5 && grounded)
    {

        GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpHeight);

    }
}

You should be using Input.GetAxis("Horizontal") . Here is an easy code sample:

public float maxSpeed = 10f;
public float jumForce = 700f;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGrounded;
bool grounded = false;


void FixedUpdate()
{
    grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGrounded)

    float move = Input.GetAxis("Horizontal");

    rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
}

void Update()
{
    if(grounded &&  Input.GetKeyDown(KeyCode.Space))
    {
        rigidbody2D.AddForce(new Vector2(0, jumpForce));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM