using System; using System.Collections; using UnityEngine; using Random = UnityEngine.Random; namespace MAVRIC.GEEKCup.Portraits { public class LookAtCamera : MonoBehaviour { private Transform lookTarget; private Vector3 lookGoal; public float maxLookDiff = 0.5f; public float randLookSpeedMin = 0.1f; public float randLookSpeedMax = 1.0f; public float randMinChangeLook = 0.2f; public float randMaxChangeLook = 2.0f; private void Start() { StartCoroutine(WaitForCamera()); IEnumerator WaitForCamera() { while (Camera.main == null) yield return null; lookTarget = Camera.main.transform; StartCoroutine(Looking()); } } private IEnumerator Looking() { while (true) { yield return null; int randDirection = Random.Range(0, 3); float randSpeed = Random.Range(randLookSpeedMin, randLookSpeedMax); float randAngle = Random.Range(0, maxLookDiff); Vector3 goal = lookTarget.position; Quaternion startRot = transform.localRotation; switch (randDirection) { case 0: goal += lookTarget.right * randAngle; break; case 1: goal += lookTarget.up * randAngle; break; case 2: goal -= lookTarget.right * randAngle; break; case 3: goal -= lookTarget.up * randAngle; break; default: break; } transform.LookAt(goal); Quaternion lookRot = transform.localRotation; transform.localRotation = startRot; for (float timer = 0; timer < randSpeed; timer += Time.deltaTime) { float percent = timer / randSpeed; transform.localRotation = Quaternion.Lerp(startRot, lookRot, percent); yield return null; } transform.localRotation = lookRot; yield return new WaitForSeconds(Random.Range(randMinChangeLook, randMaxChangeLook)); } } } }