Пример #1
0
        private Vector3 FindRepositioningTargetAndGo()
        {
            NavMeshAgent navAgent = ai.navAgent;
            Vector2      moveTo;

            float radius = wanderingAreaRadius;

            for (int i = 0; i < 3; i++)
            {
                NavMeshPath path;
                moveTo = searchPoint + Random.insideUnitCircle * radius;
                if (navAgent.CanReach(moveTo, out path))
                {
                    navAgent.SetPath2D(path);
                    return(moveTo);
                }
                radius *= 0.5f;
            }

            moveTo = searchPoint + Random.insideUnitCircle * wanderingAreaRadius;
            NavMeshHit hit;

            if (navAgent.Raycast2D(moveTo, out hit))
            {
                moveTo = hit.position;
            }
            if (!navAgent.pathPending)
            {
                navAgent.SetDestination2D(moveTo);
            }
            return(moveTo);
        }
Пример #2
0
    public static bool SetDestination2D(this NavMeshAgent nmAgent, Vector2 destination, bool startInstantly = false)
    {
        if (!nmAgent.isActiveAndEnabled)
        {
            return(false);
        }
        float z             = nmAgent.GetAgentZ();
        var   destination3D = new Vector3(destination.x, destination.y, z);

        if (startInstantly)
        {
            var path = new NavMeshPath();
            if (nmAgent.CalculatePath(destination, path))
            {
                nmAgent.SetPath2D(path);
            }
        }

        /*NavMeshHit hit;
         * if(NavMesh.SamplePosition(destination3D, out hit, 1f, NavMesh.AllAreas)) {
         *  destination3D = hit.position;
         * }*/
#if UNITY_EDITOR
        var helper = nmAgent.GetComponent <NavAgentHelper>();
        if (helper != null)
        {
            helper.lastDestination2D = destination3D;
        }
#endif
        return(nmAgent.SetDestination(destination3D));
    }