why is the rigidbody's velocity zero in OnCollisionEnter2D?
My game object moves correctly but when it collides its velocity is zero. I know relative velocity is right but I want the velocity not the relative velocity between two objects. I wrote :
Rigidbody2D rigidbody;
void Start () {
rigidbody = GetComponent<Rigidbody2D>();
}
void Update{
rigidbody.velocity=Vector2.one;
}
void OnCollisionEnter2D(Collision2D coll){
// here rigidbody.velocity is zero !!!
}
i see no problem with your code except :
Rigidbody2D rgb;
void Start ()
{
rgb = GetComponent<Rigidbody2D>();
}
Yes I wrote it. rigidbody = GetComponent(); coll.relativeVelocity is right but rigidbody.velocity is zero into OnCollisionEnter2D!
Answer by aditya · Mar 05, 2016 at 07:51 AM
it is because you are checking the velocity of rigidbody after the collision, unity calls OnCollisionEnter2D after the collision and it is obvious that after collision the velocity is going to be ZERO
thank you but I need the velocity value of the object when it collides. what do I do. why is the relative velocity right and works. is it appropriate that I store the previous velocity every frame and use it.
Yes you had to save the previous velocity using FixedUpdate or Update