0
\$\begingroup\$

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;
     }
\$\endgroup\$
0
\$\begingroup\$

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);
\$\endgroup\$
6
  • \$\begingroup\$ I will try this and let you know how it works. Thank you very much for your help thus far. \$\endgroup\$ – cyo Mar 3 '19 at 0:14
  • \$\begingroup\$ Would I be putting 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); in the update function? \$\endgroup\$ – cyo Mar 3 '19 at 0:21
  • \$\begingroup\$ Note that the paragraph above says "inside Update" \$\endgroup\$ – DMGregory Mar 3 '19 at 0:38
  • \$\begingroup\$ and the ellipses represent which parts of the code? Apologies for the clarification on my end. \$\endgroup\$ – cyo Mar 3 '19 at 0:43
  • \$\begingroup\$ Everything between the start of the function and where you do your clamping \$\endgroup\$ – DMGregory Mar 3 '19 at 0:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.