250421 TIL

박소희·2025년 4월 21일

Unity_7기

목록 보기
73/94

NPC 행동 변경

public void GuardWait()
{
    var agent = stateMachine.npc.Agent;

    if (!isWaiting)
    {
        waitTimer += Time.deltaTime;
        moveTimer += Time.deltaTime;

        if (moveTimer >= moveDelay)
        {
            agent.SetDestination(GetRandomPointInArea(stateMachine.npc.Area));
            moveTimer = 0f;
        }

        bool isMoving = !agent.pathPending && agent.remainingDistance > agent.stoppingDistance;
        if (isMoving) StartAnimation("Walk");
        else StopAnimation("Walk");

        if (waitTimer >= 3f)
        {
            if (stateMachine.npc is Guard guard)
            {
                agent.SetDestination(guard.GetWaitPosition().transform.position);
                isWaiting = true;
                waitTimer = 0f;
            }
        }
    }
    else // 대기중
    {
        if (!agent.pathPending && agent.remainingDistance <= agent.stoppingDistance)
        {
            StopAnimation("Walk");
            cooldownTimer += Time.deltaTime;

            if (cooldownTimer >= 5f)
            {
                isWaiting = false;
                cooldownTimer = 0f;
                waitTimer = 0f;
                agent.SetDestination(GetRandomPointInArea(stateMachine.npc.Area));
            }
        }
    }
}

처음부터 제대로 알려주지

0개의 댓글