- Home /
How to check which colliders that collision with some object ?
Hai guys. I have a problem with my car racing game especially in collider for NPC. I have 4 colliders to trigger some action in my NPC but I'm still don't know how to check which collider that collision with some object.
The two collider in front of car are use to make decision in my NPC that want to avoid the obstacle in front of the car. If the right one hit the obstacle, the car will turn left to avoid the obstacle and vice versa. But if both of them hit the obstacle, the NPC will activate the two collider beside the car and check the obstacle again for making decision that the NPC will turn left or right.
This is a picture about my NPC which have 4 colliders.
Thanks
Syahroyni
Thanks
Syahroyni
Answer by flashframe · Jan 28, 2016 at 06:51 PM
Instead of having four colliders on one game object, try attaching a collider to four separate child objects.
Then write a script with an OnTriggerEnter() function that calls a method in your main script and passes the name of the child object. (You'll need to reference the main object's script in your child objects).
//in script on child object
public MainScript mainScript //whatever your script is called - attach in inspector
void OnTriggerEnter()
{
mainScript.CarImpact("LeftTrigger");
}
//in main script
public void CarImpact(string trigger)
{
switch(trigger)
{
case "LeftTrigger":
//do something
break;
}
}
Yeaaa. Its work. Thank you flash frame. But, I have one question again. How if the trigger collider hit an obstacle in same time ?
Answer by Vagonn · Jan 28, 2016 at 04:38 PM
Attach this script to car
public class Car()
{
void OnTriggerEnter(Collider other)
{
Debug.Log(other.name);
}
}
Yeahh, I already try this before, but the other name is the wall's collider not my car's collider.
Answer by Astraphobia95 · Jan 28, 2016 at 05:38 PM
Check out the collision functions for the Monobehaviour class, you'll need to write some scripts to get what you want to work.
yeahh, I already write the script. I use OnTriggerEnter. But that method doesn't give me the name of car's collider that hit the wall. That method return the name of wall collider.