- Home /
Different scene depending on level completion time remaining?
Hi guys,
I'm trying to incorporate a function where a player must reach an area within a particular time limit. I've got my timer script set up so when it reaches zero, a 'gameover' scene is loaded:
var startTime = 90.0;
var timeLeft : int;
function Update () {
//Time left
timeLeft = startTime - Time.time;
timeLeft = Mathf.Max (0, timeLeft);
guiText.text = FormatTime (timeLeft);
if (timeLeft == 0) {
print ("Time is up!");
Application.LoadLevel("gameover");
}
}
Now I'm trying to figure out how i can add a function so when the 'player' collides with the 'portal' with say, 20 seconds left to spare, a scene indicating that they'd won a 'gold medal' would load up. If they instead happened to finish with 10 seconds left to spare, then a different scene would load up indicating that they had won a 'silver medal' etc. Overall, I'm aiming to produce three different end-of-level scenes (Gold, Silver, Bronze).
I have a script attached to the 'portal' gameobject which currently identifies if the 'player' collides with it, to then apply a Application.LoadLevel function. Of course, with this new function I am trying to implement, I'm assuming i would need to pass some kind of if statement through? This is the script after various attempts of getting the newly proposed function to work:
var timeLeft : int;
function OnCollisionEnter (theCollision : Collision) {
if(theCollision.gameObject.name == "player"){
(timeLeft > 20) {
print ("Gold!");
Application.LoadLevel("gold-medal-scene");
}
}
}
//if (timeLeft > 10) {
//print ("Silver!");
//Application.LoadLevel("silver-medal-scene");
The commented out section is what I am assuming is the way I would get the function to work, but the problem i have is that I already have an if statement applied to the OnCollisionEnter script to identify the collection detection. :/
So I'm writing to ask for your advice and help as to how I could pass this proposed function within the script that I have. Any guidance would be greatly appreciated!
Answer by BiG · Apr 02, 2012 at 05:31 PM
You can use if-statements enclosed into other ones. I would modify your script as follows, assuming that you win a gold medal for 20+ secs, a silver one for 10+ secs, and a bronze one for finishing the level with less than 10 secs.
var timeLeft : int;
function OnCollisionEnter (theCollision : Collision) {
if(theCollision.gameObject.name == "player"){
if (timeLeft > 20) {
print ("Gold!");
Application.LoadLevel("gold-medal-scene");
}
else if (timeleft > 10){
print ("Silver!");
Application.LoadLevel("silver-medal-scene");
}
else if (timeleft > 0){
print ("Bronze!");
Application.LoadLevel("bronze-medal-scene");
}
}
}
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Delaying through yield 2 Answers
How to write a simple save script 1 Answer
Loadlevel when you press a key? 3 Answers