x


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!

more ▼

asked Apr 02 '12 at 05:26 PM

stve101 gravatar image

stve101
2 1 2

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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");
       }    
   }
}
more ▼

answered Apr 02 '12 at 05:31 PM

BiG gravatar image

BiG
4.7k 4 13 49

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5070
x572
x263

asked: Apr 02 '12 at 05:26 PM

Seen: 352 times

Last Updated: Apr 02 '12 at 05:31 PM