I commented out the line with rb.velocity and gravity went back to normal. I’m trying to create snappy movement for a 2D platformer so if anyone has a fix or some alternate syntax I could use, that would be great! Here is my code:
public Rigidbody2D rb;
void Update()
{
float moveHorizontal = Input.GetAxisRaw("Horizontal");
Vector2 movement = new Vector2(moveHorizontal, 0.0f);
//for some reason this next line causes problems with gravity calculations
rb.velocity = movement * speed;
}
TreyH
2
(edited to format your code)
That’s because gravity itself works by changing a Rigidbody’s velocity. By setting it manually in Update, you’re going to observe a stutter because the physics cycle recalculates what velocity should be independent of your Update calls.
To resolve, keep track of your gravity’s influence. This example will assume you’re using traditional gravity along the Y-axis, but you can extend this to use gravity in any direction.
void Update()
{
float moveHorizontal = Input.GetAxisRaw("Horizontal");
var velocity = rb.velocity;
velocity.x = moveHorizontal * speed;
rb.velocity = velocity;
}