230 lines
5.1 KiB
C#
230 lines
5.1 KiB
C#
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<NavMeshAgent>();
|
|
|
|
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<Collider>();
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
} |