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"; [Header("Collision")] public bool disableCollidersWhenSitting = true; public Collider[] hookColliders; [Header("Test")] public Key testStartKey = Key.T; private NavMeshAgent agent; private int step = 0; private bool isMoving = false; private bool hasArrived = false; private bool isSitting = false; private Vector3 currentDestination; void Start() { agent = GetComponent(); if (agent != null) { agent.isStopped = true; agent.ResetPath(); agent.stoppingDistance = 0.05f; agent.autoBraking = true; } if (animator != null) { animator.SetBool(walkBoolName, false); } // Inspector¿¡ Collider¸¦ ¾È ³Ö¾îµµ ÀÚµ¿À¸·Î ÈÄÅ© ÀÚ½Ä ÄݶóÀÌ´õµéÀ» ã¾ÆÁÜ if (hookColliders == null || hookColliders.Length == 0) { hookColliders = GetComponentsInChildren(); } } void Update() { // Å×½ºÆ®¿ë: T ´©¸£¸é Ãâ¹ß if (Keyboard.current != null && Keyboard.current[testStartKey].wasPressedThisFrame) { StartWalking(); } 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 || hasArrived || isSitting) { return; } step = 0; hasArrived = false; isMoving = true; if (animator != null) { 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; // 1. NavMeshAgent ¸ÕÀú ²ô±â if (agent != null) { agent.isStopped = true; agent.ResetPath(); agent.enabled = false; } // 2. ÈÄÅ© ¸ö Collider ²ô±â // ÀÇÀÚ/Å×À̺í ÄݶóÀÌ´õ¿¡ ¹Ð·Á¼­ À§·Î ¿Ã¶ó°¡´Â ¹®Á¦ ¹æÁö 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.SetTrigger(sitTriggerName); } Debug.Log("Hook moved to sit point and started sitting."); } void DisableHookColliders() { if (hookColliders == null || hookColliders.Length == 0) { return; } foreach (Collider col in hookColliders) { if (col != null) { col.enabled = false; } } } void StopWalking() { isMoving = false; if (agent != null && agent.enabled) { agent.isStopped = true; agent.ResetPath(); } if (animator != null) { animator.SetBool(walkBoolName, false); } } }