Files
WhaleAdventure_VR/Assets/02_Scripts/Blackjack/HookWalkToTable.cs
2026-06-16 16:46:52 +09:00

312 lines
7.1 KiB
C#

using System.Collections;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.InputSystem;
public class HookWalkToTable : MonoBehaviour
{
[Header("Path Points")]
public Transform tableFrontPoint;
public Transform chairFrontPoint;
[Header("Sit Point")]
public Transform sitPoint;
[Header("Move Setting")]
public float arriveBuffer = 0.15f;
[Header("Animator")]
public Animator animator;
public string walkBoolName = "isWalking";
public string sitTriggerName = "sitTrigger";
public string standTriggerName = "standTrigger";
[Header("Stand Up Setting")]
public float standUpDuration = 1.5f;
[Header("Collision")]
public bool disableCollidersWhenSitting = true;
public bool enableCollidersAfterStandUp = true;
public Collider[] hookColliders;
[Header("Test")]
public Key testStartKey = Key.T;
public Key testStandUpKey = Key.Y;
private NavMeshAgent agent;
private int step = 0;
private bool isMoving = false;
private bool hasArrived = false;
private bool isSitting = false;
private bool isStandingUp = false;
private Vector3 currentDestination;
void Start()
{
agent = GetComponent<NavMeshAgent>();
if (agent != null)
{
agent.isStopped = true;
agent.ResetPath();
agent.stoppingDistance = 0.05f;
agent.autoBraking = true;
}
if (animator != null)
{
animator.SetBool(walkBoolName, false);
animator.ResetTrigger(sitTriggerName);
animator.ResetTrigger(standTriggerName);
}
if (hookColliders == null || hookColliders.Length == 0)
{
hookColliders = GetComponentsInChildren<Collider>();
}
}
void Update()
{
// 테스트용: T 누르면 테이블로 이동 후 앉기
if (Keyboard.current != null && Keyboard.current[testStartKey].wasPressedThisFrame)
{
StartWalking();
}
// 테스트용: Y 누르면 일어나기
if (Keyboard.current != null && Keyboard.current[testStandUpKey].wasPressedThisFrame)
{
StandUp();
}
if (!isMoving || agent == null)
{
return;
}
if (agent.pathPending)
{
return;
}
float distance = Vector3.Distance(transform.position, currentDestination);
if (distance <= agent.stoppingDistance + arriveBuffer)
{
GoNextStep();
}
}
public void StartWalking()
{
if (agent == null)
{
Debug.LogWarning("NavMeshAgent is missing.");
return;
}
if (tableFrontPoint == null || chairFrontPoint == null)
{
Debug.LogWarning("TableFrontPoint or ChairFrontPoint is missing.");
return;
}
if (isMoving || isSitting || isStandingUp)
{
return;
}
step = 0;
hasArrived = false;
isMoving = true;
EnableHookColliders();
if (animator != null)
{
animator.ResetTrigger(sitTriggerName);
animator.ResetTrigger(standTriggerName);
animator.SetBool(walkBoolName, true);
}
agent.enabled = true;
agent.isStopped = false;
MoveTo(tableFrontPoint);
Debug.Log("Hook starts walking to table front point.");
}
void MoveTo(Transform target)
{
NavMeshHit hit;
if (NavMesh.SamplePosition(target.position, out hit, 1.5f, NavMesh.AllAreas))
{
currentDestination = hit.position;
agent.SetDestination(currentDestination);
Debug.Log("Destination set: " + target.name);
}
else
{
Debug.LogWarning("Target is not near NavMesh: " + target.name);
StopWalking();
}
}
void GoNextStep()
{
step++;
if (step == 1)
{
MoveTo(chairFrontPoint);
Debug.Log("Hook moves to chair front point.");
}
else
{
ArriveAndSit();
}
}
void ArriveAndSit()
{
isMoving = false;
hasArrived = true;
isSitting = true;
isStandingUp = false;
// 1. 이동 중지
if (agent != null)
{
agent.isStopped = true;
agent.ResetPath();
agent.enabled = false;
}
// 2. 앉아 있는 동안 후크 콜라이더 끄기
if (disableCollidersWhenSitting)
{
DisableHookColliders();
}
// 3. 실제 앉을 위치로 강제 이동
if (sitPoint != null)
{
transform.position = sitPoint.position;
transform.rotation = sitPoint.rotation;
}
else if (chairFrontPoint != null)
{
transform.rotation = chairFrontPoint.rotation;
}
// 4. 앉는 애니메이션 실행
if (animator != null)
{
animator.SetBool(walkBoolName, false);
animator.ResetTrigger(standTriggerName);
animator.ResetTrigger(sitTriggerName);
animator.SetTrigger(sitTriggerName);
}
Debug.Log("Hook moved to sit point and started sitting.");
}
public void StandUp()
{
if (!isSitting || isStandingUp)
{
return;
}
StartCoroutine(StandUpRoutine());
}
IEnumerator StandUpRoutine()
{
isStandingUp = true;
isSitting = false;
if (animator != null)
{
animator.SetBool(walkBoolName, false);
animator.ResetTrigger(sitTriggerName);
animator.ResetTrigger(standTriggerName);
animator.SetTrigger(standTriggerName);
}
Debug.Log("Hook started stand up animation.");
yield return new WaitForSeconds(standUpDuration);
if (enableCollidersAfterStandUp)
{
EnableHookColliders();
}
if (agent != null)
{
agent.enabled = true;
agent.isStopped = true;
agent.ResetPath();
}
hasArrived = false;
isStandingUp = false;
Debug.Log("Hook stood up.");
}
void DisableHookColliders()
{
if (hookColliders == null || hookColliders.Length == 0)
{
return;
}
foreach (Collider col in hookColliders)
{
if (col != null)
{
col.enabled = false;
}
}
}
void EnableHookColliders()
{
if (hookColliders == null || hookColliders.Length == 0)
{
return;
}
foreach (Collider col in hookColliders)
{
if (col != null)
{
col.enabled = true;
}
}
}
void StopWalking()
{
isMoving = false;
if (agent != null && agent.enabled)
{
agent.isStopped = true;
agent.ResetPath();
}
if (animator != null)
{
animator.SetBool(walkBoolName, false);
}
}
}