I have this code set up on my player to limit how fast they can go:
private void FixedUpdate()
{
//Moves the player up to top speed. if greater than the top speed, doesnt add force.
if (inputLR > 0 && rb2d.velocity.x < topSpeed)
{
rb2d.AddForce(Vector2.right * inputLR * moveSpeed);
}
else if (inputLR < 0 && rb2d.velocity.x > -topSpeed)
{
rb2d.AddForce(Vector2.right * inputLR * moveSpeed);
}
}
I have used this code on a similar project (in unity 2018.3.9f1) and it worked perfectly for me, allowing me to reach exactly what the top speed is set to no problem. But now, I used that same line of code again (in unity 2019.1.7f1) and I am getting velocities of varying amounts. I have no clue what is going wrong!
If I set the top speed to 12, the player will speed up to a velocity of 11-14 and maintain that velocity until the button is let go. This velocity that it decides to maintain seems completely random.
Is there something I am doing wrong here? Should I just scrap this code and redo it differently?