简体   繁体   中英

My character's horizontal movement is stopping me from using rigidBody2D.AddForce(Vector2.right) or any horizontal axis

I keep trying to Addforce to the left or right but nothing happens. This happened to me in another game before. I just figured out what it was

rb.velocity = new Vector2(speed * moveX * Time.deltaTime, rb.velocity.y);

Here, I'm setting the velocity to 0 if I'm not moving (moveX is Input.GetAxisRaw("Horizontal") ) How do I AddForce horizontally while not having to disable my normal movement? I know I can use transform.Translate but I'd prefer using rb.velocity for now.

With the add force function, link to unity docs below.

https://docs.unity3d.com/ScriptReference/Rigidbody2D.AddForce.html

I have faced this several times but

  • if you are using AddForce what you can do is either make sure to add a good amount of speed value to it

    rb.AddForce(inputMove.x * speed);
  • if you are using Time.deltatime or don't use time at all...something like:

     rb.AddForce(inputMove.x * Time.deltatime * speed);

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