Respawn in a scene

In scene 1, is the world. there you play the actual game. Then you can enter scene 2 when you press the “e” button to change settings in scene 1.

my question is: how do you respawn in the exact position where you pressed the “e” button in Scene 1?

EDIT:
this is a code I think would work, but something is wrong. can someone help me fix it?

using UnityEngine;
using System.Collections;

public class playerscript : MonoBehaviour {

Vector3 originalposition;
Quaternion originalrotation;

// Use this for initialization
void Start () {
	
	originalposition = transform.position;
	originalrotation = transform.rotation;
	

}

// Update is called once per frame
void Update () {
	
	
	if(Input.GetKeyDown("e"))
	{
		
	Application.LoadLevel("the cave");	
		
	}
	

}


void Reset () {
	
transform.position = originalposition;
transform.rotation = originalrotation;
	
}

}

1 Answer

1

You would want to save the position of the player just as they pressed “e” in Scene one, then recall that same position when the player presses “e” in scene 2.

Pseudo code:
While in Scene 1:

When "e" is pressed{
     save 'respawn' = player's current coordinates; // may need to set this as a static variable so you can recall it from another script
     move player to Scene 2;
}

While in Scene 2:

When "e" is pressed{
     move player to Scene 1;
     move player to 'respawn';
}

You may want to add the ability for the player to respawn in Scene 2 just like they can in Scene 1, you would simply add similar lines to the code controlling scene 2:

Pseudo code:
While in Scene 1:

When "e" is pressed{
     save 'respawnScene1' = player's current coordinates; // may need to set this as a static variable so you can recall it from another script
     move player to Scene 2;
          if respawnScene2 != 0{
          move player to 'respawnScene2;
     }
}

While in Scene 2:

When "e" is pressed{
     save 'respawnScene2' = player's current coordinates; // may need to set this as a static variable so you can recall it from another script
     move player to Scene 1;
          if respawnScene1 != 0{
          move player to 'respawnScene1;
     }
}

***** Edit after you OP added code*****

I’m not the best at this, but something like:


using UnityEngine;
using System.Collections;

public class playerscript : MonoBehaviour {

Vector3 originalposition;
Quaternion originalrotation;

// Use this for initialization
void Start () {

    If (transform.position != null) {  //checks to see if you've stored the position of the player before (something that would have been done if you pressed "e" to leave a scene, this is here so when you return to the scene it knows to load your player's position)
    transform.position = originalposition; 
    transform.rotation = originalrotation;
    }

}

// Update is called once per frame
void Update () {

    if(Input.GetKeyDown("e"))
    {

    originalposition = transform.position;
    originalrotation = transform.rotation;    
    Application.LoadLevel("the cave"); 

    }

}
}

Then, when you load ‘the cave’ scene you need to run this script again so that it cycles through the “Start” section -

This may not be perfect, but this is a quick edit and I’m the most efficient coder - the logic should mostly be there.

I edited my previous answer.