AnswerBun.com

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);
         }
         else
         {
             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

Best way to save game data in Unity

1  Asked on February 28, 2021 by tair-galili

   

Unity – Sprite vertices in 3D world

1  Asked on February 24, 2021 by muckington

   

How to represent a modular FSM for AI using ECS?

2  Asked on February 22, 2021 by christian-ivicevic

       

get access to Transform of a Clone in Unity

1  Asked on February 21, 2021 by amin007

   

Box2d on authoritative server for Unity client

0  Asked on February 19, 2021 by antonio-agustin

         

How to Send Voice over Unity Networking – UNET

1  Asked on February 12, 2021 by muhammad-faizan-khan

         

Mathf.Clamp not Working Properly

1  Asked on February 3, 2021 by shubhendra-chaddha

   

SetTrigger Only Works In Start and Not OnTriggerEnter

1  Asked on February 3, 2021 by kit-k

       

How do I detect image clicks in Dark GDK?

1  Asked on February 2, 2021 by bobman

   

FIx color banding in unity

0  Asked on February 1, 2021

 

OnMouseOver not registering collisions

3  Asked on January 29, 2021 by hyperific

   

Call native code from Unity iOS build error

1  Asked on January 29, 2021 by atlantis

   

Ask a Question

Get help from others!

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