DWTD/Assets/Scripts/Player/WeaponSelectMenu.cs
2024-09-04 01:08:09 +02:00

96 lines
2.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.XR;
using static UnityEngine.GraphicsBuffer;
public class WeaponSelectMenu : MonoBehaviour
{
public Transform headTransform;
public Transform imageTransform;
private Vector3 originalScale;
private Coroutine currentTransition;
private Hand activeHand;
private void Awake()
{
originalScale = transform.localScale;
}
private void Start()
{
Hide();
}
public void Update()
{
transform.rotation = Quaternion.LookRotation(transform.position - headTransform.position);
if (activeHand != null)
{
Vector3 relative = transform.InverseTransformPoint(activeHand.transform.position);
float angle = Mathf.Atan2(relative.x, relative.z) * Mathf.Rad2Deg;
imageTransform.localRotation = Quaternion.Euler(0f, 0f, angle);
}
}
public void Show(Hand hand)
{
activeHand = hand;
transform.position = hand.transform.position;
foreach (Transform t in transform)
{
t.gameObject.SetActive(true);
}
CancelCurrentTransition();
currentTransition = StartCoroutine(Co_ScaleTransition(false, 0.3f));
}
public void Hide()
{
activeHand = null;
CancelCurrentTransition();
currentTransition = StartCoroutine(Co_ScaleTransition(true, 0.3f));
}
public void CancelCurrentTransition()
{
if (currentTransition != null)
StopCoroutine(currentTransition);
}
private IEnumerator Co_ScaleTransition(bool hide, float duration)
{
float timeElapsed = 0;
do
{
timeElapsed += Time.deltaTime;
float normalizedTime = timeElapsed / duration;
if (hide)
{
transform.localScale = Vector3.Lerp(transform.localScale, Vector3.zero, Easing.Cubic.In(normalizedTime));
}
else
{
transform.localScale = Vector3.Lerp(transform.localScale, originalScale, Easing.Cubic.Out(normalizedTime));
}
yield return null;
}
while (timeElapsed < duration);
foreach (Transform t in transform)
{
t.gameObject.SetActive(!hide);
}
}
}