- Home /
Damage by collision with ridgidbodies
Using OnCollisionEnter, how would I detect how fast a ridgidbody is moving relative to the object that it hits?
void OnCollisionEnter(Collision coll)
{
if(rigidbody)
{
print(rigidbody.velocity);
}
if(coll.rigidbody)
{
print(coll.rigidbody.velocity);
}
}
if(rigidbody) is checking if the current object with the script attached is a rigidbody, and names its velocity. coll.rigidbody is checking if the OTHER object is a rigidbody. The if statement is just to make sure the colliding object is a rigidbody. This prevents a null ref exception. So yeah if you have this script on an object, and want to check the velocity of the object it is colliding with, coll.rigidbody.velocity, or whatever you name your Collider. Say if you wanted to name it 'other':
void OnCollisionEnter(Collider other)
{
}
Answer by cmpgk1024 · Aug 08, 2013 at 07:32 PM
rigidbody.velocity
gives you the current velocity of a rigidbody.
http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-velocity.html
Thanks! Sorry if it looked like this was copying yours - I submitted $$anonymous$$e a few $$anonymous$$utes earlier but it took forever to get through moderation.
No worries man! $$anonymous$$y ego isn't so big I think ppl copy my answers lol XD That's why I gave you a Vote up :) Happy Developing!
So: void OnCollisionEnter(Collider other) { health -= ridgidbody.velocity }
Sure, but you might want to check how high the velocity is vs. your health and maybe set the health to a fraction of the velocity depending on how high it typically is.