So I have this small piece of code…
var laps : int = 0;
function OnTriggerEnter() {
laps++
}
}
How can I make a maximum limit of 5 laps and if i completed the 5 laps…then I will load another scene(Application.LoadLevel)…
So I have this small piece of code…
var laps : int = 0;
function OnTriggerEnter() {
laps++
}
}
How can I make a maximum limit of 5 laps and if i completed the 5 laps…then I will load another scene(Application.LoadLevel)…
Use an ‘if’ statement and the == operator to compare the value of ‘laps’ to the desired value.
In the block of code associated with the ‘if’ statement, use Application.LoadLevel() to load the new level.
Try that, and if you have trouble writing the code, post your attempt here so we can take a look at it.
Ok so this is what I have so far:
var laps : int = 0;
function OnTriggerEnter() {
laps++;
if (laps == 3 )
Application.LoadLevel(1);
}
Ok. Does it work?
It works…
But my level loads after 1 lap…instead of 3…
Is there any other objects in the game that are hitting the trigger, besides the player? The way you have it currently, if ANYTHING with a collider hit the trigger, laps will be increased by 1.
Sounds about right, if each tire hits that collider (along with the body of the car) that could be causing the unwanted laps = three problem. Though it also helps if you add in a debug.log expression as well so that you can see what’s going on.
var laps : int = 0;
function OnTriggerEnter() {
laps++;
Debug.Log("I've completed " + laps + " laps.")
if (laps == 3 )
Application.LoadLevel(1);
}
So we (and you) can tell if it’s being triggered by just the car or everything connected to the car.
And now the debugging process begins ![]()
To begin with, you might try adding a Debug.Log() statement that tells you when the lap counter has been incremented, and seeing if that tells you anything about why the next level appears to be loading after only one lap. (As legend411 suggested, it may be because other objects are interacting with the trigger as well.)
It’s triggered by everything connected to the car ![]()
In that case, assign one of the objects associated with the car a specific name or tag, and then check for that name/tag in your OnTriggerEnter() function and only increment the lap counter when that specific object enters the trigger.
This. For example, tag the overall car object “player”, then do something like:
function OnTriggerEnter(other : collider){
if(other.CompareTag("Player"){
laps++;
Debug.Log("I've completed " + laps + " laps.")
if (laps == 3 )
Application.LoadLevel(1);
}
}
legend411…
I tried your script…but every time I enter the Trigger,my Debug Log says ,I’ve completed 0 laps"…
Since the Log() statement follows the statement ‘laps++’, it should not be saying you’ve completed 0 laps (unless the value of ‘laps’ is being modified elsewhere, and assuming the initial value of ‘laps’ is zero).
Can you post your current code?
I used the code that legend411 posted above…
I just started scripting…and I don’t know much yet ![]()
And now…my next level won’t load after i pass ,X" times through the ,finish line"…
Wait…is it still always saying “I’ve completed 0 laps”, or has that changed?
It keeps saying ,I’ve completed 0 laps".
Post the script in its entirety (remember to use ‘code’ tags).
var laps : int = 0;
function OnTriggerEnter(other : Collider) {
if(other.CompareTag("Player"))
laps++;
Debug.Log("I've completed " + laps + " laps.");
if (laps == 3 )
Application.LoadLevel(1);
}
you forgot the curly braces for the if statement ![]()
Ok, I see what the problem is (or might be).
First, a couple of important tips:
Always use consistent indentation. If the indentation is lost when posting a code excerpt to the forums, try replacing hard tabs with spaces first (as that will generally result in the formatting being correctly preserved).
Always enclose blocks of code following a conditional or loop statement in curly brackets. This will help prevent confusion and help to avoid certain common logic errors.
Here’s your code with the formatting fixed:
var laps : int = 0;
function OnTriggerEnter(other : Collider) {
if (other.CompareTag("Player"))
laps++;
Debug.Log("I've completed " + laps + " laps.");
if (laps == 3 )
Application.LoadLevel(1);
}
What I would do next is add curly brackets (as described above), and then add a Debug.Log() statement letting you know when ‘laps’ is being incremented. My guess is that this statement will never be printed to the console, which will tell you that the conditional ‘other.CompareTag(“Player”)’ is never evaluating to ‘true’. This in turn should give you a pretty good hint as to what the problem is.
Also, you’ll probably want to re-arrange the logic to reflect the fact that if laps is not incremented, there’s no point in checking to see if the required number of laps has been completed.