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.