- Home /
OnTrigger ignoring all collisions but the first
Hi everybody:
I have a huge problem. I'm trying to implementing a waypoint AI system for my racing game. I have all the waypoints in my circuit stored in a list, and in the OnTriggerEnter(), when it arrives to the first waypoint, the target of my AI car point to the next waypoint. Also, when OnTriggerExit() is active, it add 1 to the variable that control the target to be pointed. But it doesn't works furthermore, only with the first collision. Here are my codes:
private Transform targetObject;
private int i = 0; // This variable will control the current waypoint to follow
public List<Transform> waypoints;
private KartController kart;
void Start ()
{
// keep a reference to the kart component
kart = GetComponent<KartController>();
targetObject = waypoints [i];
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Waypoint") {
targetObject = waypoints [i+1];
}
}
void OnTriggerExit(Collider other)
{
if (other.tag == "Waypoint") {
i++;
return;
}
}
}
Can you help me? Thanks a lot!
Answer by _joe_ · Mar 28, 2015 at 05:17 PM
The code that you posted works perfectly, Just make sure that your Character has a rigidbody (you can untick Use Gravity), And make sure that the Collider that is attached to your Character has "Is Trigger" Checked.