using UnityEngine; using NodeCanvas.Framework; using ParadoxNotion.Design; using NavMeshAgent = UnityEngine.AI.NavMeshAgent; using NavMesh = UnityEngine.AI.NavMesh; using NavMeshHit = UnityEngine.AI.NavMeshHit; namespace NodeCanvas.Tasks.Actions { [Category("Movement/Pathfinding")] [Description("Makes the agent wander randomly within the navigation map")] public class Wander : ActionTask { public BBParameter speed = 4; public BBParameter keepDistance = 0.1f; public BBParameter minWanderDistance = 5; public BBParameter maxWanderDistance = 20; public bool repeat = true; protected override void OnExecute() { agent.speed = speed.value; DoWander(); } protected override void OnUpdate() { if ( !agent.pathPending && agent.remainingDistance <= agent.stoppingDistance + keepDistance.value ) { if ( repeat ) { DoWander(); } else { EndAction(); } } } 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); } } protected override void OnPause() { OnStop(); } protected override void OnStop() { if ( agent != null && agent.gameObject.activeSelf ) { agent.Warp(agent.transform.position); agent.ResetPath(); } } } }