- Home /
Why doesn't Rigidbody.velocity.x = 0 in a collision?
For context, I am trying to implement a parallax effect. The parallax effect is contingent upon my player's 2d movement. When he moves, the backgrounds move at a fraction of the player's velocity. The player's velocity.x is modified through AddForce functions (impulse). However, when the player its a boxCollider2D, even though he is not moving in the scene, his velocity is still apparently non-zero. Why is this?
And if it is impossible to use Rigidbody2D.velocity to get what I need for parallax, what other methods can I use to get the player's "true" in game speed.
Answer by meat5000 · Apr 29, 2016 at 10:50 AM
There is only 1 situation in which a collision will result in velocity = 0 and that is a perfect head on collision with a totally non-moving object and only for the moment of impact. If the other object is movable then any imbalance, rotation etc will result in at least 1 axis retaining some velocity. If you really want it to be 0 on impact then you can script it to do that in the OnCollisionEnter routine.
I have scripted it to do this, but unfortunately I can either:
A.) Have my velocity.x set to 0 in CollisionEnter/Stay2D which is not perfect. The velocity fluctuates between having the velocity it would have with just AddForce and 0.
B.) Prevent movement at all in a collision, but then I cannot exit.