- Home /
Why is my character still falling when the character velocity.y is 0? (2D)
Hi! I am new to using Unity. This isn't really something important that I need fixed immediately but I feel I need to know the answer to get better at using Unity and programming in general.
void Update () {
rb.freezeRotation = true;
float moveHorizontal = Input.GetAxisRaw("Horizontal");
Vector2 movement = new Vector2(moveHorizontal, 0);
rb.velocity = movement * speed;
If I hold down the left key, I expected the character to move left without falling since my velocity.y would forever be 0. The result is my character falls off even when I press the left key.
Answer by alph1 · Apr 28, 2017 at 08:44 AM
You should check gravity scale of your attached rigidbody2D
component and set it as 0 so it wouldnt add gravity force to pull you down
Answer by _Keagan · Apr 28, 2017 at 11:27 AM
You are using Update to set the velocity of your rigidbody, so you are using Unity's frame drawing side to control movement but the Physics engine runs in a seperate thread making physics calculations potentially more often and almost always not always in sync with update. So when you set the velocity in Update the Physics Engine could run potentially mutiple times per frame altering your velocity. If you want to turn gravity off you can use this:
Physics2D.gravity = 0.0f;
Your answer
Follow this Question
Related Questions
Smooth movement using Rigidbody2d 3 Answers
Character won't move (Fixed) 1 Answer
Problem with mooving sprites 1 Answer
How to make a Grid movement (tile per tile) 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers