LD50/Assets/Scripts/CameraFollow.cs
2022-04-05 02:09:50 +02:00

89 lines
3.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : Manager<CameraFollow>
{
public GameObject FollowObject;
public GameObject FollowObjectGameOver;
public float DampingRotationFac;
public float DampingPositionFac;
public float DampingRotationFacGameOver;
public float DampingPositionFacGameOver;
private Vector3 targetLocation;
private Quaternion targetRotation;
public AnimationCurve EasingPositionCurve;
public AnimationCurve EasingRotationCurve;
public AnimationCurve EasingPositionCurveGameOver;
public AnimationCurve EasingRotationCurveGameOver;
public void OnGameStateChanged(GameState newState, GameState oldState)
{
if(newState == GameState.Play)
{
updateTarget();
updateCameraLerp(1.0f, 1.0f);
}
if(newState == GameState.GameOver)
{
}
}
protected void Start()
{
GameStateManager.Instance.OnGameStateChangedEvent.AddListener(OnGameStateChanged);
}
protected void OnDestroy()
{
GameStateManager.Instance.OnGameStateChangedEvent.RemoveListener(OnGameStateChanged);
}
private void updateCameraLerp(float lerpRot, float lerpPos)
{
this.transform.rotation = Quaternion.Lerp(this.transform.rotation, targetRotation, lerpRot);
this.transform.position = Vector3.Lerp(this.transform.position, targetLocation, lerpPos);
}
private void updateTarget()
{
GameObject activeFollowTarget = GameStateManager.Instance.GameState == GameState.Play ? FollowObject : FollowObjectGameOver;
Quaternion lookRotation = Quaternion.identity;
lookRotation.SetLookRotation(Vector3.Scale(activeFollowTarget.transform.forward, new Vector3(1.0f, 0.0f, 1.0f)).normalized, Vector3.up);
targetLocation = activeFollowTarget.transform.position;
targetRotation = lookRotation;
}
protected void OnGameOverAnimationFinished()
{
Debug.Log("OnGameOverAnimationFinished OKAY GO GO GO ");
}
protected void FixedUpdate()
{
updateTarget();
float activeDampingRotationFactor = GameStateManager.Instance.GameState == GameState.Play ? DampingRotationFac : DampingRotationFacGameOver;
float activeDampingPositionFactor = GameStateManager.Instance.GameState == GameState.Play ? DampingPositionFac : DampingPositionFacGameOver;
float tRot = Mathf.Clamp(Mathf.Abs(Quaternion.Angle(this.transform.rotation, targetRotation)) * activeDampingRotationFactor, 0.0f, 1.0f);
float tPos = Mathf.Clamp((this.transform.position - targetLocation).magnitude * activeDampingPositionFactor, 0.0f, 1.0f);
AnimationCurve activeRotationCurve = GameStateManager.Instance.GameState == GameState.Play ? EasingRotationCurve : EasingRotationCurveGameOver;
AnimationCurve activePositionCurve = GameStateManager.Instance.GameState == GameState.Play ? EasingPositionCurve : EasingPositionCurveGameOver;
tRot = activeRotationCurve.Evaluate(tRot);
tPos = activePositionCurve.Evaluate(tPos);
updateCameraLerp(tRot, tPos);
}
}