Enter / Exit Trigger

Hey guys,

I am currently writing a script for a racing game that keeps track of how many checkpoints a racer had passed. The checkpoints are triggers and when a racer enters (OnTriggerEnter)a trigger they get 1 added to their checkPointsReached.If a racer decides to go backwards through a trigger a checkpoint is decremented when they exit(OnTriggerExit) the trigger.

The problem I am having is when a racer slightly enters a trigger then exits out the same way. For example if a racer is heading toward checkpoint 3 - slightly enters incrementing the checkpoints to 4 but then leaves the same way, the script will be searching for checkpoint 4 when in fact the racer would be heading toward checkpoint 2.
The same happens if a racer passes a checkpoint , say checkpoint 5 then turns around and slightly re-enters checkpoint 5 but then turns back the correct way , the OnTriggerExit will activate and decrement the number reached even though the racer is going the correct way.

I could double up every checkpoint , give them a little space and have each one respond to a separate trigger, however, on some stages there are quite a large amount so would become very tedious.

I have also experimented with having the script only check every second checkpoint for going the wrong way but it causes problems at the start and end and results in very convoluted code.

Any help is appreciated!

Thank you

Jeff

Hi Jeff, I am a bit confused as to what you think OnTriggerExit and OnTriggerEnter do. are those checkpoints static in the world?
if they are just a box trigger on the track as soon as the car drives over the trigger, OnTriggerEnter is called, when it leaves the box OnTriggerExit is called. that might confuse your script. I would suggest that you have a little script attached to your checkpoint that contains a boolean iWasPassed:false. you could then set that one to true in your OnTriggerEnter function and have an if statement to check whether it is already checked or not. I write some code here but i cant test it. this is for your checkpoint:

var iWasPassed : booelan = false;
function OnTriggerEnter(other : Collider){
  iWasPassed=!iWasPassed //this sets the boolean to the opposite state. first time to true.
   if(iWasPassed){
    other.transform.GetComponent(CarScript).checkPointsReached++;
  }
  else{
     other.transform.GetComponent(CarScript).checkPointsReached--;
  }

}

This assumes that the script that contains the variable checkPointsReached is attached to the car and called CarScript…

I hope that helps somewhat

I ended up adding an extra checkpoint behind every checkpoint .The first checkpoint incremented the checkpoints reached and the second decremented. Bools were used to check if it was appropriate to increment/decrement i.e if a racer had gone through the first check point a front bool is set to true then if they carry on and go through the second checkpoint it would check to see if front was true before incrementing/decrementing. I used OnTriggerEnter for incrementing and OnTriggerExit for decrementing to stop any confusion.