-1

I have two GameObjects Body1 and Body2, Both have separate BoxCollider. i want to identify which GameObject (Body1 or Body2) have hit the other.

Muhammad Jazab
  • 24
  • 2
  • 11

1 Answers1

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