This commit is contained in:
mischa 2023-12-13 11:34:33 +01:00
parent 4d74a803c8
commit ba20b07fbc

View File

@ -8,25 +8,33 @@ public class Player : NetworkBehaviour
public Transform cameraMount; public Transform cameraMount;
Vector3 initialCameraPosition; Vector3 initialCameraPosition;
Quaternion initialCameraRotation; Quaternion initialCameraRotation;
Camera cam;
protected virtual void Awake()
{
// find main camera once
cam = Camera.main;
}
public override void OnStartLocalPlayer() public override void OnStartLocalPlayer()
{ {
// remember initial camera position/rotation // remember initial camera position/rotation
initialCameraPosition = Camera.main.transform.position; initialCameraPosition = cam.transform.position;
initialCameraRotation = Camera.main.transform.rotation; initialCameraRotation = cam.transform.rotation;
// move main camera into camera mount // move main camera into camera mount
Camera.main.transform.SetParent(cameraMount, false); cam.transform.SetParent(cameraMount, false);
Camera.main.transform.localPosition = Vector3.zero; cam.transform.localPosition = Vector3.zero;
Camera.main.transform.localRotation = Quaternion.identity; cam.transform.localRotation = Quaternion.identity;
} }
public override void OnStopLocalPlayer() public override void OnStopLocalPlayer()
{ {
// move the camera back to the original point // move the camera back to the original point.
Camera.main.transform.SetParent(null, true); // otherwise it would be destroyed when stopping the game (and player)
Camera.main.transform.position = initialCameraPosition; cam.transform.SetParent(null, true);
Camera.main.transform.rotation = initialCameraRotation; cam.transform.position = initialCameraPosition;
cam.transform.rotation = initialCameraRotation;
} }
} }
} }