I have two GameObjects
Body1
and Body2
, Both have separate BoxCollider
. i want to identify which GameObject
(Body1
or Body2
) have hit the other.
Asked
Active
Viewed 402 times
-1

Muhammad Jazab
- 24
- 2
- 11
-
http://answers.unity3d.com/questions/400393/detect-collision-between-object-and-box-collider.html – Sushant Poojary Jul 29 '17 at 11:02
-
https://unity3d.com/learn/tutorials/topics/physics/detecting-collisions-oncollisionenter – Sushant Poojary Jul 29 '17 at 11:02
1 Answers
0
void OnCollisionEnter (Collision col)
{
Debug.Log(col.gameObject.name);
}
With this you can get the name of the object. But if i got your question correct, both of them have collider and body1
hits body2
is quite same with body2
hits body1
You can make the discrimination with, if one of them is standing still (no position changed) and other one is moving, then you can say body1
hits body2
or whatever your condition is.
To achive this you can use a bool, like isObjectMoving
. You can check it in Update()
. If transform.position
is changing bool will be true and you will check it in if statement
void OnCollisionEnter (Collision col)
{
if(col.gameObject.getComponent<YourBoolScriptName>().isObjectMoving == true)
//Debug.Log(the other body hit me);
}
Hope this helps! Cheers!

Thalthanas
- 496
- 6
- 10