- Home /
Storing Position of First Person Camera
I've run into an issue. In my game, I have a First Person Camera, and when a trigger is hit, a different scene is loaded. At the end of that scene, I need to return to the previous scene and have the camera in the same position and facing the same direction.
I've attempted to store the position and rotation of the camera, and when I return, it seems to work. But, when I start to move the camera, it goes completely out of whack (which makes me believe it isn't moving everything necessary).
How would I store the entire First Person Camera's location and rotation and then utilize that information when I return to the scene so that the player is dropped right back into the same place that they left?
DontDestroyOnLoad will be your solution. http://docs.unity3d.com/Documentation/ScriptReference/Object.DontDestroyOnLoad.html
void Awake() {
DontDestroyOnLoad(transform.gameObject);
}
Answer by ProphetSword · Nov 15, 2013 at 01:19 AM
Unfortunately, that didn't work. Using "DontDestroyOnLoad" carries it into the new scene, and then when I go back into the old scene, I have two copies of the First Person Camera.
That said, I did get it to work by doing this (calling "storeCamera" before leaving the scene and calling "retrieveCamera" when returning to the scene).
public GameObject FPCamera;
Vector3 cameraPosition;
Quaternion cameraRotation;
public void storeCamera () {
// Save coordinates:
cameraPosition = FPCamera.gameObject.transform.position;
cameraRotation = FPCamera.gameObject.transform.rotation;
}
public void retrieveCamera () {
// Load coordinates:
FPCamera.transform.position = cameraPosition;
FPCamera.transform.rotation = cameraRotation;
}