Hi, I have some problems with trigger. I’m doing car racing game and wanna add lap counting. I have a car with top_collider and bottom_collider, I have empty game object=finish, The problem is when car goes through gameObject it counts 2times, both collider, how to make it count only one for example paying attention for just bottom_collider? and how to display it correctly?
var lap: int;
var startTime = 0;
var timeTaken: float;
Hi!
One way would be to set a flag in the OnTriggerEnter function;
function OnTriggerEnter(hit : Collider)
{
if (Time.Realtimesincestartup-lastlaptime>minlaptime){
if(hit.gameObject.tag == "Finish")
{
lastlaptime=Time.Realtimesincestartup;
lap++;
}
}
}
By using a timer like this, you don´t have to worry about several colliders triggering the event.
You obviously need to declare and initialize your lastlaptime and minlaptime variables.
I would also create another checkpoint somewhere along the track to make it harder to cheat…
sorry, I cant understand it all, I’m not sure about “You obviously need to declare and initialize your lastlaptime and minlaptime variables” should I do var lastlaptime…? or this is another gameObject? I’m a beginner and dont feel very well in programming, please be understanding
Yes, you should “do a var lastlaptime” and var minlaptime in the same script.
and
function Start(){
lastlaptime=Time.Realtimesincestartup ;//dont remember the exact capitalization, could be RealTimeSinceStartup
minlaptime=20f; // or whatever is a resonable minimum time for your lap
}