Search Unity

Teleport a Player to other scene to a specific point

Discussion in 'Scripting' started by Twoonebe, Feb 27, 2020.

  1. Twoonebe

    Twoonebe

    Joined:
    Mar 30, 2013
    Posts:
    174
    Hi Guys, i have a problem with my change level script.

    For exampel i have at the moment two scenes worldmap and scene two world1.0. I wan`t to teleport the player on a trigger zone by pressing a key on the keyboard out to the worldmap
    upload_2020-2-27_22-21-37.png


    on a gameobject point called forest spawn point
    upload_2020-2-27_22-19-48.png

    Here is my script.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7. public class ChangeLevel : MonoBehaviour
    8. {
    9.     public Transform SpawnPosition;
    10.     public Transform Hero;
    11.     public GameObject LoadingText;
    12.     public string SceneToLoad;
    13.  
    14.     public Image LoadingBar;
    15.     public Text txt;
    16.  
    17.     /*
    18.     public void Play()
    19.     {
    20.         StartCoroutine(LoadGameScene());
    21.     }
    22.  
    23.     IEnumerator LoadGameScene()
    24.     {
    25.         AsyncOperation result = SceneManager.LoadSceneAsync(SceneToLoad);
    26.         result.allowSceneActivation = false;
    27.         while (!result.isDone)
    28.         {
    29.             float progress = Mathf.Clamp01(result.progress / 0.9f);
    30.             LoadingBar.fillAmount = progress;
    31.             txt.text = "Loading progress: " + (progress * 100) + "%";
    32.             yield return null;
    33.         }
    34.     }
    35.     */
    36.     void Start()
    37.     {
    38.         LoadingText.SetActive(false);
    39.         Hero = GameObject.FindWithTag("Player").transform;
    40.        
    41.     }
    42.  
    43.  
    44.  
    45.     void OnTriggerStay(Collider Wall)
    46.     {
    47.         if (Wall.gameObject.tag == "Player")
    48.         {
    49.             LoadingText.SetActive(true);
    50.             if (Input.GetButtonDown("Use"))
    51.             {
    52.                 SceneManager.LoadScene(SceneToLoad);
    53.                 Hero.transform.position = SpawnPosition.position;
    54.             }
    55.         }
    56.     }
    57.  
    58.     void OnTriggerExit(Collider Wall)
    59.     {
    60.         if (Wall.gameObject.tag == "Player")
    61.         {
    62.             LoadingText.SetActive(false);
    63.            
    64.         }
    65.     }
    66.    
    67. }
    My problem is the player character spawn on the point for 1 second after them they teleport back to the point of the transform from the changelevel script from the other scene
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    20,204
    As soon as the player spawns, either pause the editor, or execute a Debug.Break() somewhere, which will also pause the editor.

    This gives you a chance to study the scene and perhaps see if there is something amiss.

    If you can't identify what's going on, it might be necessary to make two EXTREMELY simple versions (completely new simple scenes) of your world and your level, put the scripts in there, stick the player in and try to study whats in the game at that point when you spawn.
     
  3. Twoonebe

    Twoonebe

    Joined:
    Mar 30, 2013
    Posts:
    174
    I found the problem but i can not solve that.

    The teleport spawn me on the right point on the "Forestspanwpoint". But only for 1 second after them it teleports me back in the same scene to the coordinates of the teleport object in the other scene
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    20,204
    Check out your lines 52 and 53 above... unfortunately the scene does not load until the NEXT frame.

    This means you need some kind of simple coroutine to do the scene load, then yield one frame, and then do the set spawn point, something like:

    Code (csharp):
    1. IEnumerator Onwards()
    2. {
    3.   // load the scene here...
    4.  
    5.   yield return null;
    6.  
    7.   // set the spawnpoint here
    8. }
    The MOST TRICKY PART about this is that the script MUST go on a GameObject that survives into the next scene. Otherwise, the second half of the coroutine will never run.
     
  5. Twoonebe

    Twoonebe

    Joined:
    Mar 30, 2013
    Posts:
    174
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7. public class ChangeLevel : MonoBehaviour
    8. {
    9.     //LevelManager
    10.     public Transform SpawnPosition;
    11.     public Transform Hero;
    12.     public GameObject TriggerMessage;
    13.     public string SceneToLoad;
    14.    
    15.     //Loading Image an Progress
    16.     public Image LoadingBar;
    17.     public Text Loadingtext;
    18.  
    19.     void Start()
    20.     {
    21.         TriggerMessage.SetActive(false);
    22.         Hero = GameObject.FindWithTag("Player").transform;
    23.     }
    24.  
    25.     void OnTriggerStay(Collider Wall)
    26.     {
    27.         if (Wall.gameObject.tag == "Player")
    28.         {
    29.             TriggerMessage.SetActive(true);
    30.             if (Input.GetButtonDown("Use"))
    31.             {
    32.                 StartCoroutine("StarleveltoLoad");
    33.                 //SceneManager.LoadScene(SceneToLoad);
    34.                 //Hero.transform.position = SpawnPosition.position;
    35.  
    36.             }
    37.         }
    38.     }
    39.    
    40.  
    41.     void OnTriggerExit(Collider Wall)
    42.     {
    43.         if (Wall.gameObject.tag == "Player")
    44.         {
    45.             TriggerMessage.SetActive(false);
    46.            
    47.         }
    48.     }
    49.  
    50.     IEnumerable StarleveltoLoad ()
    51.     {
    52.         SceneManager.LoadScene(SceneToLoad);
    53.  
    54.         yield return null;
    55.  
    56.         Hero.transform.position = SpawnPosition.position;
    57.     }
    58.  
    59.  
    60.  
    61. }

    i have write a corountines but now i can not teleport :-(
     
  6. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    990
    i would create simple non gamemanager, non co-routine version of implementation first to check its working. Then when happy work step by step to integrate it together.
     
unityunity