Page 1 of 1

Loading a Level

Posted: Tue Dec 07, 2010 5:58 am
by SweetRevenge
Hey guys! I've been gone for a while, college and stuff..... But I've decided to take on a smaller project, a 2D arcade style game.. sort of like mario. I'm having a problem figuring out how to a level with Unity 3 now. I have a platform, and when the players steps on it, I want it to load the next level. Can anyone help me with this? I know i will need to use Application.LoadLevel(2)

Re: Loading a Level

Posted: Tue Dec 07, 2010 11:58 pm
by SpiritWebb
This is the code I use to load different star systems.

Here is the code: (note: this is for a phase gate, but just modify the code to work for you!)

Phase Gate Code:

Code: Select all

private var activated = false;

var mySkin : GUISkin;
var TraderMenuButton : GUIStyle;

var windowWidth : int = 300;
var windowHeight : int = 400;

var toggle = true;

private var showMenu : boolean = false;

function isActivated() {
   return activated;
}

function OnMouseDown() {
   showMenu = true;
   activated = true;
}

function OnGUI () {
GUI.skin = mySkin;
    if (showMenu) {

        GUI.Window(0,

                   Rect((Screen.width / 2) - (windowWidth / 2),

                        (Screen.height / 2) - (windowHeight / 2),

                        windowWidth,

                        windowHeight),

                   Gate,

                   "Choose your destination");  //Creates a window with ID 0 in the center of the screen using the function LevelSelect with the title, "Select a level!"

    }

}

function Gate (id : int) {

      if(GUILayout.Button("Loc'Tor IV")){
         Application.LoadLevel("Loc'Tor IV");
         }
      GUILayout.Button("Thetia");
      
if (GUILayout.Button("Cancel")) {  //Gives the player the opportunity to back out of the menu.

        showMenu = false;
      activated = false;

    }

}
Phase Gate Controller

Code: Select all


private var litAmount = 0.00;

function Start () {

   var playerController : PhaseGateEarth = GetComponent(PhaseGateEarth);
   
   // The script ensures an AudioSource component is always attached.
   
   // First, we make sure the AudioSource component is initialized correctly:
   audio.loop = false;
   audio.Stop();
   
   
   // Init the particles to not emit and switch off the spotlights:
   var particles : Component[] = GetComponentsInChildren(ParticleEmitter);
   var childLight : Light = GetComponentInChildren(Light);
   
   for (var p : ParticleEmitter in particles)
   {
      p.emit = false;
   }
   childLight.enabled = false;

   // Once every frame  update particle emission and lights
   while (true)
   {
      var isOn = playerController.isActivated();
            
      // handle thruster sound effect
      if (isOn)
      {
         if (!audio.isPlaying)
         {
            audio.Play();
         }
      }
      else
      {
         audio.Stop();
      }
      
      
      for (var p : ParticleEmitter in particles)
      {
         p.emit = isOn;
      }
      
      if(isOn)
         litAmount = Mathf.Clamp01(litAmount + Time.deltaTime * 2);
      else
         litAmount = Mathf.Clamp01(litAmount - Time.deltaTime * 2);
      childLight.enabled = isOn;
      childLight.intensity = litAmount;
               
      yield;
   }
}

@script RequireComponent(AudioSource)
Remember to make sure you change the name of the scripts. If you change PhaseGateEarth to DoorOpen, then the PhaseGateControllerEarth needs to be changed to DoorOpenController and make sure that you change in the controller where it says PhaseGateEarth to DoorOpen or whatever you named it or it won't work. Also, you can import an audio file, and attach it too the controller and it will play a sound, for example maybe a door opening sound. HINT HINT!

Enjoy and I hope this helps!!