MouseLook Script "Pops" back to the last value when the script is enabled after being disabled or destroyed

Game Development Asked by cyo on January 7, 2022

I am trying to get a problem with a MouseLook script that controls my camera and a LookatGameObject script that also controls my camera when I want to force the player to look in a certain direction under control, but still no luck. I have identified where the issue is – Line 43, transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0); is the the method that sets the X Axis, makes it “POP” back to the last value when the script is enabled (I have even destroyed the component and then added the component when needed but the same result) can anyone see a solution for this?

The MouseLook Script:

 using UnityEngine;
 using System.Collections;

 [AddComponentMenu("Camera-Control/Mouse Look")]
 public class MouseLook : MonoBehaviour

     public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
     public RotationAxes axes = RotationAxes.MouseXAndY;
     public float sensitivityX = 15F;
     public float sensitivityY = 15F;

     public float minimumX = -360F;
     public float maximumX = 360F;

     public float minimumY = -60F;
     public float maximumY = 60F;

     float rotationY = 0F;

     void Update()
         if (axes == RotationAxes.MouseXAndY)
             float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;

             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
             rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);

             transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
         else if (axes == RotationAxes.MouseX)
             transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
             rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);

             transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);

Here is my LookAtGameObjectScript:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class LookAtGameObject : MonoBehaviour
     public Transform target;
     public float speed = 5f;

     // Update is called once per frame
     void Update()

         Vector3 direction = target.position - transform.position;
         Quaternion rotation = Quaternion.LookRotation(direction);
         transform.rotation = Quaternion.Lerp(transform.rotation, rotation, speed * Time.deltaTime);

And And my method for turning off MouseLook and Turning on LookAtGameObject:

private IEnumerator LookAtGameObject()

         GameObject.FindWithTag("MainCamera").GetComponent<MouseLook>().enabled = false;
         GameObject.FindWithTag("MainCamera").GetComponent<LookAtGameObject>().enabled = true;

         yield return new WaitForSeconds(8f);

         GameObject.FindWithTag("MainCamera").GetComponent<LookAtGameObject>().enabled = false;
         GameObject.FindWithTag("MainCamera").GetComponent<MouseLook>().enabled = true;

         yield return null;

One Answer

It looks like all you need is an OnEnable method to update the internal state when the script is switched back on:

float lastEnabledTime = float.negativeInfinity;
float limitEaseSeconds = 3f;

void OnEnable() {
    rotationY = transform.localEulerAngles.y;
    lastEnabledTime = Time.time;

Saving the last enabled time lets us gradually re-introduce our rotation limits inside Update, by first calculating our current limits for this frame:

float limitBlend = Mathf.Clamp01((Time.time - lastEnabledTime)/limitEaseSeconds);

float currentMin = Mathf.Lerp(-180, minimumY, limitBlend);
float currentMax = Mathf.Lerp(180, maximumY, limitBlend);


rotationY = Mathf.Clamp(rotationY, currentMin, currentMax);

Answered by DMGregory on January 7, 2022

Add your own answers!

Related Questions

Convert a string to a gameobject name

1  Asked on December 1, 2021 by stormzy18


Jumping up quicker? (Paper Mario style jump)

1  Asked on December 1, 2021 by spottel


A* Pathfinding Finding One of Many Goals

1  Asked on November 29, 2021 by user6256


Wheel rotation for a raycast car in Unity?

1  Asked on November 29, 2021 by john-545


Unity “scrape away” effect

0  Asked on November 24, 2021 by darren-swords


Initial setting of camera and ppu to match playing table

1  Asked on November 21, 2021 by zibelas


LibGDX – Tilemap Wall Collision Detection Issues

2  Asked on November 20, 2021 by james-stoddern


Tilemaps not showing up on Android build

1  Asked on November 18, 2021 by jcorretjer


Unity: Sprite stretch issue

1  Asked on November 14, 2021 by tikeb


Unity UI, how to hide on play

2  Asked on November 12, 2021


Alternative path not working

1  Asked on November 12, 2021 by dsngl-tibor


Unity – Understanding the SRP batcher

2  Asked on November 12, 2021 by majs


Ask a Question

Get help from others!

© 2023 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP