- Home /
 
Why moving characters laterally with addForce fails?
I am using the following code to move my character laterally:
 // Get input
 float move = Input.GetAxis ("Horizontal");
 
 // Set speed
 Vector2 _velocity = rigidbody2D.velocity;
 _velocity.x = move * walkSpeed;
 rigidbody2D.velocity = _velocity;
 
               Which works just fine. I am using this code to handle jumping:
 if (grounded && Input.GetKeyDown (KeyCode.Space)) {
     grounded = false;
     rigidbody2D.AddForce(new Vector2(0, jumpForce));
 }
 
               Which also works fine. So suddenly I thought, I could maybe do this to move the character laterally:
 // Get input
 float move = Input.GetAxis ("Horizontal");
 // Set speed
 rigidbody2D.AddForce(new Vector2(move * walkSpeed, 0));
 
               But this does NOT work. The character remains in the same place always.
Could someone explain me the failure in my logic?
Answer by Owen-Reynolds · Feb 22, 2015 at 05:57 PM
Probably a scale problem. velocity is simple -- sets your speed to that many meters/second. AddForce is funny -- it divides the speed by about 60 (it has to do with ForceMode. It assumes you're going to be using it 60 times/second.) In your project, jumpSpeed is probably way more than walkSpeed (it really makes more sense to set velocity for a jump, anyway.)
Try cranking walkSpeed to 1000.
Your answer
 
             Follow this Question
Related Questions
Movement with AddForce: Wrong Direction 2 Answers
move 2d character affected by physics with velocity and/or add force 2 Answers
Push an object opposite direction of mouse position 0 Answers
My object can't move left and right but only jump? 1 Answer
Add Force To One Object in Relation to the Rotation of Another Object (C#) 1 Answer