1
0
mirror of https://projects.caleb-brown.dev/UDRI-XRT/UDRIGEEKCup2024.git synced 2025-01-22 07:08:51 -05:00
UDRIGEEKCup2024/Assets/Portraits/LookAtCamera.cs
2024-04-23 13:47:11 -04:00

85 lines
2.5 KiB
C#

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