Unity 3D Door System (scene change) C #

1

I am developing a project in Unity and I have come to a doubt. I put you in situation: There is a village with different houses, the character can access those houses through the door, approaches it, collides with a Trigger and presses a key and enters the interior of the house, loading a new scene .

When the player wants to leave the interior of the house he approaches the door that contains another trigger and presses a key, loads the scene again and appears in the village.

Here comes my doubt, since I can not get it in front of the door, it always ends up establishing the default position when loading the scene. How could I do it? The name of the scene and the coordinates in which I want the character to appear are assigned by Unity itself.

The code I have made is the following:

public bool trigger;
public string escenaFinal; //nombre de la escena
public Vector3 coord;

void Start(){

}

void OnTriggerEnter(Collider other) {
    if (other.tag == "Player") {

        trigger = true;    //Trigger true

        }
    }

void OnTriggerExit(Collider other){

    if (other.tag == "Player") { //Sale del trigger falso

        trigger = false;

    }
}

void Update(){

}

void OnGUI(){
    if (trigger) {
        GUI.Box (new Rect (0, 60, 200, 25), "Presiona K"); //Estamos en trigger
        if(Input.GetKeyDown(KeyCode.K)){ //Si está dentro del Trigger y pulsa la letra K
            GameObject.FindGameObjectWithTag("Player").transform.position = coord;
            SceneManager.LoadScene(escenaFinal); //Cargamos la escena
            GameObject.FindGameObjectWithTag("Player").transform.position = coord;
        }
    }
}

The GameObject.FindGameObjectWithTag("Player").transform.position = coord; part is where you should set the assigned position but it does not. Thank you and I await your answers.

    
asked by Alvaro Lechuga 19.06.2018 в 13:53
source

1 answer

0

This problem came up to me, what I did was to use the PlayerPreferences that has unity 3D, in a global script, load the PlayerPreferences, and when you enter a house, you keep the position x, y, z of the door of entry, and when you leave, the first thing you do is verify those coordinates to move the character there once the scene is loaded.

using UnityEngine;
using UnityEngine.SceneManagement;

public class Example : MonoBehaviour
{
    void Start()
    {
        //Así guardas las coordenadas
        PlayerPrefs.SetFloat("ultimacasavisitada_x", 10.3f);
        PlayerPrefs.SetFloat("ultimacasavisitada_y", 54.03f);
        PlayerPrefs.SetFloat("ultimacasavisitada_z", 0f);
    }

    void salirDeCasa(){
        //Así recoges los valores
        float x = PlayerPrefs.GetFloat("ultimacasavisitada_x", 0);
        float y = PlayerPrefs.GetFloat("ultimacasavisitada_y", 0);
        float z = PlayerPrefs.GetFloat("ultimacasavisitada_z", 0);

        //Creamos un Vector3 y movemos al personaje instantaneamente a esa coordenada
    }
}

I leave the link to the document for more info: link

    
answered by 19.06.2018 в 14:54