- Home /
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
Answer by jeff · Jun 21, 2012 at 02:27 PM
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.
Nice work! $$anonymous$$ark it as answered to get it off this list :)
Answer by pheash · Jun 17, 2012 at 07:46 PM
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
Hey Pheash,
Thanks for your answer. I feel this may have the same problem. If the car enters the trigger for the first time iWasPassed will be false and checkPointsReached will be incremented. That works well ,however, what if the car turns around and re-enters the checkpoint but does not fully go through? iWasPassed will be true and checkPointsReached will be decremented but because the car didnt fully go through the checkpoint they wouldnt re-trigger it and would carry on with checkPointsReached being one less than it should.
It is a very rare case when a player is driving properly but on occasion if a wall is hit and something strange happens the problem arises.
How about checking an already_entered boolean when the trigger is entered and don't do the code if it is already set. You could then have another trigger somewhere to reset the checkpoints boolean.
Hey whydoitdoit,
Do you mean to have one trigger to reset all checkpoints or one for each checkpoint? Also what if they decide to go backwards before that trigger resets ?
Thanks for help
hey Jeff, I see what you mean, maybe you want to make the checkpoint colliders smaller, for example the bit on the car that collides with the checkpoint could be rather small, it doesnt have to be the cars boxcollider. just make an extra object that is parented to the car but is only about the size of a wheel. I know the above problem could still occur but that way it would be very improbable. the only other way that comes to my $$anonymous$$d would be to check whether the car is to the left or right of the checkpoint once it comes into a certain range. this would mean you need to check the distance between car and checkpoint and if its small enough check the direction the of the car compared to the direction of the checkpoint. that way you would have to orient your checkpoints to all point along the track.