Attach the script below to your player, and update the player prefab. It saves the current position and rotation in static variables, which survive even if the player is destroyed in one level and recreated in the other (static variables are created at program start and live while the game is running). When entering the trigger which loads the other level, set the variable restoreInfo to true: supposing this script is saved as KeepInfo.js, in the OnTriggerEnter event include the instruction KeepInfo.restoreInfo = true;
static var lastPos: Vector3;
static var lastRot: Quaternion;
static var restoreInfo = false;
function Start(){
if (restoreInfo){ // only restore info if coming from the neighbour level
transform.position = lastPos;
transform.rotation = lastRot;
restoreInfo = false;
}
}
function Update(){ // keep info updated
lastPos = transform.position;
lastRot = transform.rotation;
}
EDITED:
If the two levels are not contiguous in world space, you must have a reference to correct the position. The best way is to mark two points - point1 and point2 - which correspond to the same locations in the two levels. The easiest way to define and use these points is:
1- Modify the script loadScene like below (variables point1 and point2 were included) and save it. Modifications in code affect all instances. so you don’t need to update the players.
2- Load level 1 in the Editor (don’t press play) and move your player to some point of level 1 where it is about to change to level 2;
3- Take note of its position - this is point 1;
4- Load level 2 without saving level 1 (to preserve the initial player position) and move your player to the position it should appear coming from point1;
5- Take note of its position too - this is point2;
6- In the Inspector, fill point1 and point2 with the values you’ve noted;
7- Save the level;
8- If the player can go back to level 1, load level 1 againd and fill point1 and point2 with the noted values swapped: variable point1 receives coordinates of noted point 2, variable point2 receives noted point 1;
static var lastPos: Vector3;
static var lastRot: Quaternion;
static var restoreInfo = false;
var point1: Vector3; // position where it was in the other level
var point2: Vector3; // position it must appear in this level
function Start(){
if (restoreInfo){ // only restore info if coming from the neighbour level
transform.position = lastPos+point2-point1; // adjust position
transform.rotation = lastRot;
restoreInfo = false;
}
}
function Update(){ // keep info updated
lastPos = transform.position;
lastRot = transform.rotation;
}