내일배움캠프/TIL

[내배캠][Unity6기][TIL] NavMesh 경로 설정 수정

binary는 호남선 2024. 12. 6. 21:27

여전히 문제는 해결되지 않았지만 튜터님이 예상되는 원인을 찾아주셔서 해결 중이다!
오브젝트 풀에서 다시 생성될 때 제대로 초기화되지 않는 부분이 있는 것 같다고 조언해 주셔서 찾아보고 있다.

그 외에도 Walk나 Run에서 경로를 설정할 때 매번 경로를 재설정하지 않고 변경되었을 때만 재설정하도록 코드를 수정했다. 훨씬 효율적으로 동작하도록 코드를 변경해주셨다!

 

[ 이전 코드 ]

    public bool ArriveToDestination(Vector3 target)
    {
        agent.ResetPath();
        agent.SetDestination(target);
        bool flag = agent.remainingDistance <= agent.stoppingDistance;
        if (!flag)
            StartCoroutine(ReturnHumanProcess());
        return flag;
    }

[ 변경된 코드 ]

    public bool ArriveToDestination(Vector3 target)
    {
        if (oldTarget != target)
        {
            oldTarget = target;
            NavMeshPath path = new NavMeshPath();
            agent.CalculatePath(target, path);
            agent.SetPath(path);
        }

        bool flag = agent.hasPath;
        if (!flag)
            StartCoroutine(ReturnHumanProcess());
        return flag;
    }