How to make a Player Spwan at the same point at a different scean depending on where The player Triggered the LoadScean
Hello Guys
I am making a Student project based on the Idea of haveing Different environments for the same Level.
Basically what i wanted to do was to create a 2 scenes where a player can choose between 2 aesthetics.
In simple algorithm what i want the game to do is
There is a scean where a player can anytime trigger a script to chang how the map looks
The Level will basically be the same (geometrically ) except some objects will be added to the level and textures will change.
and the player can trigger that back .
at all points his position in the map remains the same.
How can i basically do that ?
Answer by aFeesh · Feb 18, 2017 at 04:44 AM
If I understand your question correctly you want to load a new scene, but have the player in the same position as he was right before the new scene loaded.
If that is the case you can store the player position into a static Vector3 right before the scene changes, and then set the player position immediately on void Start() to the static Vector3 value.
Like so:
private static Vector3 spawnPosition = new Vector3(0, 0, 0); // Change the X Y Z values to wherever you have the player in the FIRST scene
void Start(){
transform.position = spawnPosition;
}
public void SavePlayerPosition(){ // Call this before the level changes!
spawnPosition = transform.position;
}
Simply create a new script with that code and attach it to your player. Static variables will persist until the game is terminated. So by storing the player position in a static variable it can be saved and accessed for when the next level loads.
thank You sir !
Could you also give me some book that can halp me program better for unity 3D i am co$$anonymous$$g from C++ scripting and
i am more used to creating my own header files which can be called the "Using Unity" type files is there a way i can do that in Unity and CS scripting that can be really helpful