# Keep first person camera global rotation while the player rotates

I have a player with a camera set as its child. The script on the camera takes care of rotating the camera on the x axis while the script on the player rotates the player on the y axis (and the camera along with it). It works well, but I have a gravity system with planets and the player can change the center of gravity by clicking it (so its attracted to the object the player clicked).

The problem is that when I click on the object, the players rotates to relative to the new gravity center, and so does the camera. I wish that when my player rotates, the camera keep facing the same way. Here is what I mean.

Thats what happen when I chaneg the gravity center from the planet to the ground. Here is what I would like to happen I understand why that happens, but I don’t know how to fix it.

Here is the script on the camera:

`` void Update() {             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;     rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);     transform.localEulerAngles = new Vector3(-rotationY, 0, 0); } ``

Here is the script on the player managing the rotation around the gravity center + the Y rotation for the camera:

``if (Physics.Raycast(rayStart, GetDirection(IsGrounded, GravityChangeHitPoint), out RaycastHit hit, Mathf.Infinity, layerMask))     {         Quaternion matchSurface;         if (GravityChangeHitPoint == GravityCenter.position)             matchSurface = PlayerLookRotation(transform.forward, hit.normal);         else             matchSurface = PlayerLookRotation(transform.forward, -GetDirection(IsGrounded, GravityChangeHitPoint));           Quaternion twist = Quaternion.Euler(0, Input.GetAxis("Mouse X") * sensitivityX, 0);         if (!IsGrounded)             transform.rotation = Quaternion.Lerp(transform.rotation, matchSurface, Time.deltaTime * damping) * twist;                      else             transform.rotation = matchSurface * twist;                  }   ``

PlayerLookRotation():

``Quaternion PlayerLookRotation(Vector3 approximateForward, Vector3 exactUp) {     Quaternion zToUp = Quaternion.LookRotation(exactUp, -approximateForward);     Quaternion yToZ = Quaternion.Euler(90, 0, 0);      return zToUp * yToZ; } ``
Posted on Categories Free Proxy