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?