save position and cam rotation
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
|
||||
public class CameraController : MonoBehaviour
|
||||
{
|
||||
public float camTargetRotation { get; private set; } = 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>
|
||||
/// Set cam rotation immediately
|
||||
/// </summary>
|
||||
public void SnapToRotation(float rotation)
|
||||
{
|
||||
playerCamHome.RotateAround(
|
||||
transform.position,
|
||||
Vector3.up,
|
||||
rotation
|
||||
);
|
||||
|
||||
camTargetRotation = rotation;
|
||||
}
|
||||
|
||||
/// <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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user