- Home /
How to move a rigidbody2D at a constant speed without AddForce?
I find a lot of people telling me not to use rb.velocity to directly change the velocity since it's not the norm.
But let's say I want to create a simple Mario clone that just move Mario around. Would it be fine to change the velocity directly?
Using AddForce keeps increasing Mario's velocity. I just want him to move at a constant speed (i.e either -1, 0 or 1)
Answer by MelvMay · Mar 13, 2017 at 08:40 AM
The only reason not to set the velocity directly is if you don't take existing velocity into account. Existing velocity is affected by gravity and other forces you may apply. If you take these into account when you change the velocity then it isn't a problem. You'll also need to be careful if you're constantly applying lots of sideways forces. If you do this then you can find that the sideways force dominates gravity and when against a vertical edge, you'll seemingly stick to it as you're "pushing" against it.
That however isn't a problem if you perform simple checks for collision either checking that you're not against an edge by looking at the collision normal on the OnCollision callback or by using the GetContacts calls and checking the normal there. You could also perform simple Cast functions to check contact of the player and its colliders in the direction you want to go.
Best way, try it!
Your answer
Follow this Question
Related Questions
,Rotate a gameobject around another while being attracted by its gravity 1 Answer
Simple question about unity 5 rigidbody/gravity 1 Answer
Rigidbody2D returning zero values 2 Answers
2D rigidbody velocity relative to rotation? 0 Answers
How can I get 2 rigidbody2d objects to "share" physics? 1 Answer