DWTD/Assets/Scripts/Player/WeaponSelectMenu.cs

96 lines
2.4 KiB
C#
Raw Permalink Normal View History

2024-09-02 20:06:18 +00:00
using System.Collections;
using System.Collections.Generic;
2024-09-03 23:08:09 +00:00
using Unity.VisualScripting;
2024-09-02 20:06:18 +00:00
using UnityEngine;
using UnityEngine.InputSystem;
2024-09-03 23:08:09 +00:00
using UnityEngine.XR;
using static UnityEngine.GraphicsBuffer;
2024-09-02 20:06:18 +00:00
public class WeaponSelectMenu : MonoBehaviour
{
2024-09-03 23:08:09 +00:00
public Transform headTransform;
public Transform imageTransform;
2024-09-02 20:06:18 +00:00
2024-09-03 23:08:09 +00:00
private Vector3 originalScale;
private Coroutine currentTransition;
2024-09-02 20:06:18 +00:00
2024-09-03 23:08:09 +00:00
private Hand activeHand;
2024-09-02 20:06:18 +00:00
2024-09-03 23:08:09 +00:00
private void Awake()
2024-09-02 20:06:18 +00:00
{
2024-09-03 23:08:09 +00:00
originalScale = transform.localScale;
2024-09-02 20:06:18 +00:00
}
2024-09-03 23:08:09 +00:00
private void Start()
2024-09-02 20:06:18 +00:00
{
2024-09-03 23:08:09 +00:00
Hide();
2024-09-02 20:06:18 +00:00
}
2024-09-03 23:08:09 +00:00
public void Update()
2024-09-02 20:06:18 +00:00
{
2024-09-03 23:08:09 +00:00
transform.rotation = Quaternion.LookRotation(transform.position - headTransform.position);
if (activeHand != null)
2024-09-02 20:06:18 +00:00
{
2024-09-03 23:08:09 +00:00
Vector3 relative = transform.InverseTransformPoint(activeHand.transform.position);
float angle = Mathf.Atan2(relative.x, relative.z) * Mathf.Rad2Deg;
imageTransform.localRotation = Quaternion.Euler(0f, 0f, angle);
2024-09-02 20:06:18 +00:00
}
}
2024-09-03 23:08:09 +00:00
public void Show(Hand hand)
2024-09-02 20:06:18 +00:00
{
2024-09-03 23:08:09 +00:00
activeHand = hand;
2024-09-02 20:06:18 +00:00
2024-09-03 23:08:09 +00:00
transform.position = hand.transform.position;
foreach (Transform t in transform)
2024-09-02 20:06:18 +00:00
{
2024-09-03 23:08:09 +00:00
t.gameObject.SetActive(true);
2024-09-02 20:06:18 +00:00
}
2024-09-03 23:08:09 +00:00
CancelCurrentTransition();
currentTransition = StartCoroutine(Co_ScaleTransition(false, 0.3f));
2024-09-02 20:06:18 +00:00
}
2024-09-03 23:08:09 +00:00
public void Hide()
2024-09-02 20:06:18 +00:00
{
2024-09-03 23:08:09 +00:00
activeHand = null;
2024-09-02 20:06:18 +00:00
2024-09-03 23:08:09 +00:00
CancelCurrentTransition();
currentTransition = StartCoroutine(Co_ScaleTransition(true, 0.3f));
}
2024-09-02 20:06:18 +00:00
2024-09-03 23:08:09 +00:00
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));
}
2024-09-02 20:06:18 +00:00
yield return null;
}
2024-09-03 23:08:09 +00:00
while (timeElapsed < duration);
foreach (Transform t in transform)
{
t.gameObject.SetActive(!hide);
}
2024-09-02 20:06:18 +00:00
}
}