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. Dismiss Notice

Question How to make it teleport ?

Discussion in 'Scripting' started by Moradeusz69, Nov 18, 2021.

  1. Moradeusz69

    Moradeusz69

    Joined:
    Nov 15, 2021
    Posts:
    4
    Hi ! I'm new in unity2d and i really dont know how to make when this script goes on another scene to make player be on posX and posY i want.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class OpenDoor : MonoBehaviour
    6. {
    7.     private bool playerDetected;
    8.     public Transform doorPos;
    9.     public float width;
    10.     public float height;
    11.     public LayerMask whatIsPlayer;
    12.     SceneSwitch sceneSwitch;
    13.     public GameObject Player;
    14.  
    15.  
    16.     [SerializeField]
    17.     private string sceneName;
    18.     private void Start()
    19.     {
    20.         sceneSwitch = FindObjectOfType<SceneSwitch>();
    21.  
    22.     }
    23.    
    24.  
    25.  
    26.     private void Update()
    27.     {
    28.  
    29.  
    30.         playerDetected = Physics2D.OverlapBox(doorPos.position, new Vector2(width, height), 0, whatIsPlayer);
    31.        
    32.  
    33.         if (playerDetected == true)
    34.         {
    35.  
    36.             if(Input.GetKeyDown(KeyCode.E))
    37.             {
    38.                 sceneSwitch.SwitchScene(sceneName);
    39.              
    40.             }
    41.         }
    42.  
    43.  
    44.  
    45.  
    46.     }
    47.  
    48.     private void OnDrawGizmosSelected()
    49.     {
    50.         Gizmos.color = Color.blue;
    51.         Gizmos.DrawWireCube(doorPos.position, new Vector3(width, height, 1));
    52.     }
    53.  
    54.  
    55. }
    56.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class SceneSwitch : MonoBehaviour
    7. {
    8.     public static string prevScene;
    9.         public static string currentScene;
    10.     public virtual void Start()
    11.     {
    12.         currentScene = SceneManager.GetActiveScene().name;
    13.     }
    14.  
    15.     public void SwitchScene(string sceneName)
    16.     {
    17.         prevScene = currentScene;
    18.         SceneManager.LoadScene(sceneName);
    19.     }
    20. }
    21.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    As far as I can see, there's nothing above to do that. But usually this isn't handled by the content scene itself but via a game manager, or else by a world manager type of thing.

    One approach is to place empty GameObjects in a scene to indicate possible player spawn locations. These GameObjects would have no scripts, just be findable by name or by a tag or something.

    The GameManager would load the scene, wait for it to load, then search for the spawn locations (perhaps by name or by some other marker), select the correct one and emplace the player at that location.

    Probably best to google for ways of connecting rooms together in Unity. I think something like a binding of isaac tutorial would have information on various approaches you could try.
     
  3. Sam_Space

    Sam_Space

    Joined:
    Jun 28, 2019
    Posts:
    16
    player.transform.position = (*indcated position new Vector2(x,y)*)

    you can call a line of code like this in a function and have that function called when the scene changes.