54 lines
1.2 KiB
C#
54 lines
1.2 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
|
|
public class CameraController : MonoBehaviour
|
|
{
|
|
private float camTargetRotation = 0;
|
|
private bool isCamRotating = false;
|
|
|
|
public Transform playerCamHome;
|
|
|
|
private void Update()
|
|
{
|
|
HandleCamRotation();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Trigger cam rotation
|
|
/// </summary>
|
|
public void RotateCam()
|
|
{
|
|
if (!isCamRotating)
|
|
{
|
|
float currentY = playerCamHome.transform.rotation.eulerAngles.y;
|
|
camTargetRotation = currentY + 90f;
|
|
isCamRotating = true;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Move cam to desired rotation (every frame)
|
|
/// </summary>
|
|
void HandleCamRotation()
|
|
{
|
|
float currentY = playerCamHome.transform.rotation.eulerAngles.y;
|
|
|
|
float step = 200 * Time.deltaTime;
|
|
|
|
float newY = Mathf.MoveTowardsAngle(currentY, camTargetRotation, step);
|
|
|
|
playerCamHome.RotateAround(
|
|
transform.position,
|
|
Vector3.up,
|
|
newY - currentY
|
|
);
|
|
|
|
if (Mathf.Abs(Mathf.DeltaAngle(newY, camTargetRotation)) < 0.01f)
|
|
{
|
|
isCamRotating = false;
|
|
}
|
|
}
|
|
}
|