87 lines
2.1 KiB
C#
87 lines
2.1 KiB
C#
using UnityEngine;
|
|
|
|
public class ShellController : MonoBehaviour
|
|
{
|
|
[Header("Shell Parts")]
|
|
[Tooltip("¿¸®°í ´ÝÈ÷´Â ÀÁ¶°³ Transform")]
|
|
[SerializeField] private Transform upperShell;
|
|
|
|
[Header("Timing")]
|
|
[SerializeField] private float openDuration = 1.5f;
|
|
[SerializeField] private float closedDuration = 1.5f;
|
|
|
|
[Header("Animation")]
|
|
[SerializeField] private float closedAngle = 0f;
|
|
[SerializeField] private float openAngle = -65f;
|
|
[SerializeField] private float rotateSpeed = 8f;
|
|
|
|
[Header("Start State")]
|
|
[SerializeField] private bool startOpen = false;
|
|
|
|
public bool IsOpen { get; private set; }
|
|
public float StateTimer01 { get; private set; }
|
|
|
|
private float timer;
|
|
|
|
private void Start()
|
|
{
|
|
IsOpen = startOpen;
|
|
timer = 0f;
|
|
UpdateShellVisual(true);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
UpdateShellState();
|
|
UpdateShellVisual(false);
|
|
}
|
|
|
|
private void UpdateShellState()
|
|
{
|
|
timer += Time.deltaTime;
|
|
|
|
float currentDuration = IsOpen ? openDuration : closedDuration;
|
|
|
|
if (currentDuration <= 0f)
|
|
currentDuration = 0.1f;
|
|
|
|
StateTimer01 = Mathf.Clamp01(timer / currentDuration);
|
|
|
|
if (timer >= currentDuration)
|
|
{
|
|
IsOpen = !IsOpen;
|
|
timer = 0f;
|
|
StateTimer01 = 0f;
|
|
}
|
|
}
|
|
|
|
private void UpdateShellVisual(bool instant)
|
|
{
|
|
if (upperShell == null)
|
|
return;
|
|
|
|
float targetAngle = IsOpen ? openAngle : closedAngle;
|
|
Quaternion targetRotation = Quaternion.Euler(targetAngle, 0f, 0f);
|
|
|
|
if (instant)
|
|
{
|
|
upperShell.localRotation = targetRotation;
|
|
}
|
|
else
|
|
{
|
|
upperShell.localRotation = Quaternion.Lerp(
|
|
upperShell.localRotation,
|
|
targetRotation,
|
|
Time.deltaTime * rotateSpeed
|
|
);
|
|
}
|
|
}
|
|
|
|
public void ResetShell()
|
|
{
|
|
IsOpen = startOpen;
|
|
timer = 0f;
|
|
StateTimer01 = 0f;
|
|
UpdateShellVisual(true);
|
|
}
|
|
} |