using System.Collections; using UnityEngine; public class EyeDeviationResponse : MonoBehaviour { [SerializeField] float rDeviationThr = 7f; [SerializeField] float rFollowDeviationThr = 3f; [SerializeField] CameraEyeRotator cameraEyes = null; [SerializeField] EyeSimulation eyeSim = null; float rDevThrPow2 = Mathf.Infinity; float rFolDevThrPow2 = Mathf.Infinity; bool rDeviationRot = false; bool isReturningR = false; float firstTravelTime = 3f; float returnTime = 60f; Vector2 rDeviation = Vector2.zero; Vector2 rDevCurStart = Vector2.zero; float returningTime = 0f; void Start() { rDevThrPow2 = Mathf.Pow(rDeviationThr, 2); rFolDevThrPow2 = Mathf.Pow(rFollowDeviationThr, 2); } void Update() { if (!rDeviationRot && (Mathf.Pow(eyeSim.angleRV, 2) + Mathf.Pow(eyeSim.angleRH, 2) > rDevThrPow2)) { rDeviationRot = true; StartCoroutine(RotateCameraToDeviation()); } else if (isReturningR) { if (Mathf.Pow(eyeSim.angleRV - rDeviation.x, 2) + Mathf.Pow(eyeSim.angleRH + rDeviation.y, 2) > rFolDevThrPow2) { isReturningR = false; StartCoroutine(RotateCameraToDeviation()); } else { rDeviation = Vector2.Lerp(rDevCurStart, Vector2.zero, returningTime / returnTime); cameraEyes.SetCameraEyesRotation(Vector2.zero, rDeviation); returningTime += Time.deltaTime; } } } IEnumerator RotateCameraToDeviation() { float t = 0; Vector2 startR = rDeviation; while (t <= firstTravelTime) { t += Time.deltaTime; rDeviation.x = Mathf.SmoothStep(startR.x, eyeSim.angleRV, t / firstTravelTime); rDeviation.y = Mathf.SmoothStep(startR.y, -eyeSim.angleRH, t / firstTravelTime); cameraEyes.SetCameraEyesRotation(Vector2.zero, rDeviation); yield return null; } rDeviation.x = eyeSim.angleRV; rDeviation.y = -eyeSim.angleRH; cameraEyes.SetCameraEyesRotation(Vector2.zero, rDeviation); rDevCurStart = rDeviation; isReturningR = true; returningTime = 0f; } }