123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
-
- using UnityEngine;
- using UnityEngine.AI;
- [RequireComponent(typeof(NavMeshAgent))]
- public class RandomWalk : MonoBehaviour
- {
-
-
-
- [Tooltip("Maximum distance of the next random point to walk to.")]
- public float maxRange = 25.0f;
-
-
-
- private NavMeshAgent m_agent;
-
-
-
- private bool startWalking = false;
-
-
-
- private Vector3 destination;
-
-
-
- private uint reduceFactor = 0;
-
-
-
-
- public void Activate()
- {
- m_agent = GetComponent<NavMeshAgent>();
- m_agent.enabled = true;
- startWalking = true;
- }
- void Update()
- {
- if (startWalking)
- {
- if (m_agent.enabled)
- {
- if (m_agent.pathPending || m_agent.remainingDistance > 0.1f)
- {
- reduceFactor = 0;
- return;
- }
- destination = (Mathf.Abs(maxRange) - reduceFactor) * Random.insideUnitSphere;
- m_agent.destination = destination;
- if(reduceFactor < Mathf.Abs(maxRange)/2) reduceFactor++;
- }
- }
- }
- }
|