void DoWander()
        {
            if (!agent.isOnNavMesh)
            {
                return;
            }

            var min = minWanderDistance.value;
            var max = maxWanderDistance.value;

            min = Mathf.Clamp(min, 0.01f, max);
            max = Mathf.Clamp(max, min, max);
            var wanderPos = agent.transform.position;

            while ((wanderPos - agent.transform.position).sqrMagnitude < (min * min))
            {
                wanderPos = (Random.insideUnitSphere * max) + centerArea.value;
            }

            NavMeshHit hit;

            if (NavMesh.SamplePosition(wanderPos, out hit, float.PositiveInfinity, NavMesh.AllAreas) && agent.isOnNavMesh)
            {
                agent.SetDestination(hit.position);
            }
        }
Example #2
0
        void DoWander()
        {
            var min = minWanderDistance.value;
            var max = maxWanderDistance.value;

            min = Mathf.Clamp(min, 0.01f, max);
            max = Mathf.Clamp(max, min, max);
            var wanderPos = agent.transform.position;

            while ((wanderPos - agent.transform.position).sqrMagnitude < (min * min))
            {
                wanderPos = (Random.insideUnitSphere * max) + agent.transform.position;
            }

            NavMeshHit hit;

            if (NavMesh.SamplePosition(wanderPos, out hit, agent.height * 2, NavMesh.AllAreas))
            {
                agent.SetDestination(hit.position);
            }
        }