Home > Mobile >  My character's horizontal movement is stopping me from using rigidBody2D.AddForce(Vector2.right
My character's horizontal movement is stopping me from using rigidBody2D.AddForce(Vector2.right

Time:02-13

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.

CodePudding user response:

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

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

CodePudding user response:

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);
    
  • Related