123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- using UnityEngine;
- using UnityEngine.AI;
- using System.Collections;
-
- public class WanderingAI : MonoBehaviour
- {
- public float wanderRadius;
- public float wanderTimer;
- private Transform target;
- private UnityEngine.AI.NavMeshAgent agent;
- private float timer;
- // Use this for initialization
- void Start()
- {
- agent = this.GetComponent<UnityEngine.AI.NavMeshAgent>();
- timer = wanderTimer;
- }
- // Update is called once per frame
- void Update()
- {
- timer += Time.deltaTime;
- if (timer >= wanderTimer)
- {
- Vector3 newPos = RandomNavSphere(transform.position, wanderRadius, -1);
- Debug.Log("New Pos: " + newPos.ToString());
- agent.SetDestination(newPos);
- timer = 0;
- }
- }
- public static Vector3 RandomNavSphere(Vector3 origin, float dist, int layermask)
- {
- Vector3 randDirection = Random.insideUnitSphere * dist;
- randDirection += origin;
- UnityEngine.AI.NavMeshHit navHit;
- UnityEngine.AI.NavMesh.SamplePosition(randDirection, out navHit, dist, layermask);
- return navHit.position;
- }
- }
|