- Home /
Get force applied on a gameobject
hey, im making an open world game, and i need to get the force applied on gameobjects (eg: cars), so that the mesh (eg: part of the car) gets deformed at different amounts, when another object hits it. (btw: im using custom meshes, which will become visible when necessary)
unity obviously calculates force applied on rigidbodys (rigidbodies or whatever), so they get affected when another rigidbody pushes/hits it. can i somehow get that value (the force)?
Answer by iwaldrop · Jul 04, 2013 at 05:28 PM
Using OnCollisionEnter you can access a Collision object. The Collision object has all sorts of information about the collision in question, including the relative velocity.
void OnCollisionEnter(Collision collision)
{
Vector3 velocity = collision.relativeVelocity;
}
Other useful data that may help your use case are:
Vector3 impactForce = collision.impactForceSum;
ContactPoint[] contacts = collision.contacts;
thanks, i didnt try it yet, but this ought to work...
Answer by Bladesfist · Jul 04, 2013 at 05:17 PM
You could check the force in OnCollision with http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-velocity.html
okay, that's to change the velocity, does it "get" velocity? and btw, how can i find the force (sth which increases when 2 things collide harder)?
This can be used to either get or set the velocity. The only way I can think of that you can get the velocity that has been passed on would be to record velocity seperately on each car and then check that figure against rigidbody.velocity in OnCollision