Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Make a script that teleports the player between two scenes.

Discussion in 'Scripting' started by Ray4PREZ, Jul 18, 2015.

  1. Ray4PREZ

    Ray4PREZ

    Joined:
    Apr 13, 2014
    Posts:
    3
    I have UFPS and I am familiar with how to create new weapons and items. I want to have a weapon that is a watch (I have the models) and when you click it plays an animation and then after x amount of time it takes you to a scene.

    I have:

    Scene 1: The City
    Scene 2: The Island

    When you are in Scene 1 the teleporter will act like so:
    Get out teleporter item
    Click
    Animation plays of some energy beams or whatever
    After x amount of time you are teleported to Scene 2.

    When you are in Scene 2 the teleporter teleports you to Scene 1
    When you are in Scene 1 the teleporter teleports you to Scene 2

    Here's a completely noobish outline:

    If ("the player is in Scene 1")
    Then ("play animation, wait x amount of time then take player to scene 2")
    Else ("play animation, wait x amount of time then take player to scene 1")

    This would work because if the computer detects the player in a scene 3 or higher, it would move the player to scene 1, which would act as the hub anyways.
     
  2. aoe_labs

    aoe_labs

    Joined:
    Nov 4, 2013
    Posts:
    42
    You can try something like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TeleportScript : MonoBehaviour {
    5.  
    6.    //this is a reference to the Animator that contains the Animation you want to play
    7.     public Animator anim;
    8.  
    9.     void Start()
    10.     {
    11.        //this line is so that this object persists through each scene.
    12.        //you would put this script on the watch or any teleport device
    13.         DontDestroyOnLoad (this);
    14.     }
    15.    
    16.     void Update () {
    17.         if (Input.GetKeyDown(KeyCode.X))    //I arbitrarily chose to make all the magic happen when I press x
    18.         {
    19.  
    20.             if (Application.loadedLevel == 1) //checks if the current level is 1. this level would have to be the second item in your build settings, since the list starts at 0, so 1 is technically the second scene.
    21.             {
    22.                 StartCoroutine(Teleport(1)); //starts the teleport coroutine and passes in a value of 1 for the level. coroutines can be used for waiting x seconds.
    23.             }
    24.  
    25.             if (Application.loadedLevel == 2)  //checks if the current level is 2
    26.             {
    27.                 StartCoroutine(Teleport(2)); //passes in 2
    28.  
    29.             }
    30.  
    31.             if (Application.loadedLevel > 2) //checks if the current level is anything other than 1 or 2
    32.             {
    33.                 StartCoroutine(Teleport(1)); //passes in 1, since it's the hub
    34.             }
    35.  
    36.         }
    37.     }
    38.  
    39.     IEnumerator Teleport (int i)
    40.     {
    41.         anim.Play ("NameOfAnimation"); // first thing that happens is the animation plays. Replace NameOfAnimation with the string name of the clip you want to play.
    42.  
    43.         yield return new WaitForSeconds (3); //there is a 3 second pause
    44.        
    45.  
    46.        //then depending on what i is, the value that got passed in, (where i is equal to the current level)
    47.       // it loads the level to be teleported to
    48.         if (i == 0)
    49.             Application.LoadLevel (1);
    50.         else if (i == 1)
    51.             Application.LoadLevel (0);
    52.         else {
    53.             Application.LoadLevel (0);
    54.         }
    55.  
    56.     }
    57.  
    58.  
    59. }
    60.