- Home /
Get hit direction from one gameObject hitting another
If I fire a gun (bullet) I use a ray cast (Ray.direction) to get the direction the ray was hitting the rigid body to add force.
impactTarget = hit.rigidbody;
impact = ray.direction * 12;
I then add force to the rigid body thats been hit with
impactTarget.AddForce(impact,ForceMode.VelocityChange);
And all works well with the bullet because it's instant, if I use the Ray.direction for the RPG by the time it's reached the target the ray cast could be looking somewhere else.
But how if I shoot a RPG (gameObject with small box collider) and hit the same rigid body do I get the direction of the RPG hitting from.
ImpactTarget = contact.gameObject.rigidbody;
impact = contact.??
I'm using for the collision detection
function OnCollisionEnter(collision : Collision){
var contact = collision.contacts[0];
Thanks.
Answer by tanoshimi · Oct 01, 2014 at 09:09 PM
The "direction" between any two objects is simply the difference between their transform positions.
var Direction : Vector3 = contact.transform.position - transform.position;
You may then want to normalise this variable - it's unclear from your question exactly what you want it for.
Thanks for the input, I want it so I can use the impact direction to add force to the part of the rag dolls rigid body, arm, leg, head .. where the impact happens then
impactTarget.AddForce(impact,Force$$anonymous$$ode.VelocityChange);
I say RPG in the above example, but anything thrown hitting the body part, brick, can, ect ..
Answer by alap soni · Oct 02, 2014 at 06:43 AM
You can simply find object(game object with small box collider) by Gameobject.findbytag and the difference between the found object vector.position and impacttarget.position will give you direction .
Thank for the input, thats what tanoshimi said "The "direction" between any two objects is simply the difference between their transform positions."
Also I'me using
function OnCollisionEnter(collision : Collision)
to do all the detection.