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

How to move a character from 1 to 3 squares on a grid?

0  Asked on January 23, 2021 by shunkashuu

     

Unity 2017.3.of3 Raycast2D randomly misses objects

1  Asked on January 15, 2021 by hesitatetowonder

     

How to fix the vibration while colliding?

1  Asked on January 13, 2021

   

Render order in 3/4 view game

0  Asked on January 10, 2021 by chuan-li

 

How can I control ads frequency?

1  Asked on January 10, 2021 by ruslan-plastun

   

Attach movement to each list object

1  Asked on January 9, 2021 by mavish

     

How to make a map surface object

1  Asked on January 8, 2021 by anonymous-entity

     

Coding Spelunky Edge Roll Mechanic

0  Asked on January 5, 2021 by austin-weaver

     

Creating a lookup table of datamaps in Harlowe/Twine

0  Asked on January 4, 2021 by the_e

   

Unity 4 – Some Rigidbodies Won’t Fall Asleep

5  Asked on January 3, 2021 by ryan-berserkguard-nrby

       

Unity PackageCache keeps having errors

0  Asked on December 31, 2020 by christopher-perry

 

Phaser Tiled map importing

1  Asked on December 30, 2020 by kabuto178

     

Change the colour of all particles on screen

1  Asked on December 27, 2020 by super-potato

   

How can you make a jump to a point using the Character Controller?

0  Asked on December 25, 2020 by aimon-z

   

Trouble destroying an SDL2 texture from another class

1  Asked on December 21, 2020 by gamer1

 

How to hide objects behind an invisible plane?

1  Asked on December 18, 2020 by hatinacat2000

       

Ask a Question

Get help from others!

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