# Unity Camera snaps back to camera mouse look after lerping to a point and rotating normally. Once mouse look is re-enabled it overrides and snaps back

I am trying to get the camera to rotate towards a location and position. Which works at the moment, but once it is done and re enables the mouse look script or bool in the script the mouse look overrides the rotation and sets it back to the original rotation it had when the rotation started.

if (lockedOn)
{
// Get the direction to the target
Vector3 direction = target.transform.position - transform.position;

// Calculate the rotation to face the target
Quaternion rotationToTarget = Quaternion.LookRotation(direction, Vector3.up);

// Rotate towards the target over time
transform.rotation = Quaternion.RotateTowards(transform.rotation, rotationToTarget, rotationSpeed * Time.deltaTime);

if (currentFOV >= closeFOV)
{
curRotation = transform.rotation;
currentFOV -= fovSpeed * Time.deltaTime;
camera.fieldOfView = currentFOV;
}
}

if (!lockedOn)
{
if (currentFOV <= startFOV)
{
currentFOV += fovSpeed * Time.deltaTime;
camera.fieldOfView = currentFOV;
transform.rotation = curRotation;

if (camera.fieldOfView >= startFOV)
{
target = null;
transform.localRotation = curRotation;
mouseLook.lockControls = false;
}
}
}
if (removeFromCharacter)
{
// Distance moved equals elapsed time times speed..
float distCovered = (Time.time - startTime) * cameraMoveSpeed;

// Fraction of journey completed equals current distance divided by total distance.
float fractionOfJourney = distCovered / journeyLength;

// Set our position as a fraction of the distance between the markers.
transform.position = Vector3.Lerp(origCameraLocation.transform.position, newCameraLocation.position, fractionOfJourney);
}


and the mouse look script

 if(!lockControls)
{
// Get mouse input
float mouseX = Input.GetAxis("Mouse X");
float mouseY = Input.GetAxis("Mouse Y");

// Rotate the player's body based on horizontal mouse movement
playerBody.Rotate(Vector3.up * mouseX * sensitivity);

// Calculate the new vertical rotation
rotationX -= mouseY * sensitivity;
rotationX = Mathf.Clamp(rotationX, -verticalLookLimit, verticalLookLimit);

// Apply the rotations to the camera and player body
transform.localRotation = Quaternion.Euler(rotationX, 0f, 0f);
}


I went back and rewrote the script in the mouse look script instead of the FollowLookAt script. It is now working. Here is the script if anyone is trying to do the same:

public float sensitivity = 2.0f; // Adjust the sensitivity of mouse movement
public Transform playerBody;

public float verticalLookLimit = 90f; // Adjust the vertical look limit in degrees
private float rotationX = 0f;
public bool lockControls = false;
public GameObject targetObject;
public float movementTime = 1;
public float rotationSpeed = 0.1f;
public Transform origPostion;
public Quaternion origRotation;

public bool transitionIn;
public RemyMovementController remyMovementController;

float lookTimer;
public float lookTime = 5f;
private void Start()
{
Cursor.lockState = CursorLockMode.Locked; // Lock the cursor to the center of the screen
}

private void Update()
{
if (!lockControls)
{

// Get mouse input
float mouseX = Input.GetAxis("Mouse X");
float mouseY = Input.GetAxis("Mouse Y");

// Rotate the player's body based on horizontal mouse movement
playerBody.Rotate(Vector3.up * mouseX * sensitivity);

// Calculate the new vertical rotation
rotationX -= mouseY * sensitivity;
rotationX = Mathf.Clamp(rotationX, -verticalLookLimit, verticalLookLimit);

// Apply the rotations to the camera and player body
transform.localRotation = Quaternion.Euler(rotationX, 0f, 0f);
}
else if (lockControls)
{
if (transitionIn)
{
lookTimer += Time.deltaTime;

if (lookTimer > lookTime)
{
transitionIn = false;
lookTimer = 0;
}
Quaternion targetRotation = Quaternion.LookRotation(targetObject.transform.position - transform.position);
// Smoothly rotate towards the target point.
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
}
else if (!transitionIn)
{
transform.rotation = Quaternion.Slerp(transform.rotation, origRotation, rotationSpeed * Time.deltaTime);
if (transform.rotation == origRotation)
{
UnTrackObject();
}
}
}
}

public void TrackObject()
{
lockControls = true;
transitionIn = true;
remyMovementController.enabled = false;
origRotation = transform.rotation;
origPostion = transform;
}

public void TransitionOut()
{
transitionIn = false;
}

public void UnTrackObject()
{
lockControls = false;
remyMovementController.enabled = true;
}


With this script it allows the camera to rotate towards an object while locking the controls and then rotate back to the original position then unlock the controls.

You can add something like this to your mouse look script, so it inherits the current orientation when re-enabled:

void OnEnable() {
// Get current facing direction.
var look = transform.forward;

// Convert to spherical coordinate angles:
float yaw = Mathf.Atan2(look.x, look.z) * Mathf.Rad2Deg;
float pitch = Mathf.Asin(-look.y) * Mathf.Rad2Deg;

// Rotate player to account for yaw component:
playerBody.rotation = Quaternion.Euler(0, yaw, 0);

// Store current pitch, and apply it.
rotationX = pitch;
transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
}

• I tried this, but it flips him backwards whenever it re enables the script. I'm not sure why. He just turns around the opposite direction he was facing before. I figured it out. For the yaw, it needed to be -look.x and -look.z for me. I appreciate the help. Commented Jan 24 at 16:47
• Sounds like your model is oriented backwards. Commented Jan 24 at 17:57
• Looks like everything is in order on my side with the model. Its facing the correct way. Just unsure why it is snapping still. If I'm looking in a certain direction it snaps backwards. If I'm looking in a different direction it ignores the code and snaps back to the original location like it was before. Commented Jan 25 at 1:57