NavMeshAgent 목표포착

Unity3D/Problems 2016. 6. 9. 20:48
반응형

public SurvivorAIBot FindNearistSurvivorBot(SurvivorAIBot myBot)

    {

        var myAgent = myBot.GetBinder().navMeshAgent;

        float maxdistance = 500;

        SurvivorAIBot targetBot = null;


        foreach (var bot in this.aiSurvivorBots)

        {

            if (bot.GetProperties().uid != myBot.GetProperties().uid)

            {

                var agent = bot.GetBinder().navMeshAgent;


                //var distance = Vector3.Distance(agent.transform.position, myAgent.transform.position);


                var distance = this.CalculatePathLength(myAgent, agent.transform.position);


                if (distance < maxdistance)

                {

                    maxdistance = distance;

                    targetBot = bot;

                }

            }

        }


        Debug.Log(targetBot.GetProperties().uid);

        return targetBot;


    }


    public float CalculatePathLength(NavMeshAgent myAgent, Vector3 targetPosition)

    {

        NavMeshAgent nav = myAgent;

        NavMeshPath path = new NavMeshPath();

        if (nav.enabled)

            nav.CalculatePath(targetPosition, path);


        Vector3[] allWayPoints = new Vector3[path.corners.Length + 2];


        allWayPoints[0] = myAgent.transform.position;

        allWayPoints[allWayPoints.Length - 1] = targetPosition;


        for (int i = 0; i < path.corners.Length; i++)

        {

            allWayPoints[i + 1] = path.corners[i];

        }


        float pathLength = 0;


        for (int i = 0; i < allWayPoints.Length - 1; i++)

        {

            pathLength += Vector3.Distance(allWayPoints[i], allWayPoints[i + 1]);

        }


        return pathLength;

    }

반응형

'Unity3D > Problems' 카테고리의 다른 글

공포의 level_pattern_data  (0) 2016.07.13
GPGS Invalid classname Issue  (0) 2016.07.13
EditorGUILayout.ObjectField obsolete?  (0) 2016.06.09
C# 정확한 Type 의 비교 ( IsAssignableFrom vs is )  (0) 2016.06.09
LitJSON Limitations/Warnings  (0) 2016.05.10
: