Question by
unity_lSXJSIZAvqe3Qw · May 18, 2020 at 06:40 PM ·
navmeshagenttimerpathfindingblend treepatrol
How to make navmesh agent stop on point, and after continue his moving
Hello guys, I am trying to make enemy patrolling system, where evrytime guard reaches his point, he stopes for 10 seconds, and then continue his movement. I've tried combining animations from Blend tree with isStopped property from NavMeshAgent.
EDIT: My current script makes agent move to point, then he stopes for some time, and then only walk animation plays, but he staing on one place.
public class Patrol : MonoBehaviour
{
public Transform[] points;
private int destPoint = 0;
public NavMeshAgent agent;
public Animator animator;
public int time;
void Start()
{
agent = GetComponent<NavMeshAgent>();
animator = transform.Find("Enemy").GetComponent<Animator>();
// Disabling auto-braking allows for continuous movement
// between points (ie, the agent doesn't slow down as it
// approaches a destination point).
//agent.autoBraking = false;
GotoNextPoint();
}
void GotoNextPoint()
{
// Returns if no points have been set up
if (points.Length == 0)
return;
// Set the agent to go to the currently selected destination.
agent.destination = points[destPoint].position;
// Choose the next point in the array as the destination,
// cycling to the start if necessary.
destPoint = (destPoint + 1) % points.Length;
//agent.speed = 1f;
//animator.SetFloat("Blend", agent.speed);
}
IEnumerator DoCheck()
{
for (; ; )
{
agent.speed = 0;
agent.isStopped = true;
yield return new WaitForSeconds(2f);
}
}
void Update()
{
if (agent.remainingDistance == 0f && time == 100000)
{
agent.speed = 1f;
Debug.Log(agent.remainingDistance);
animator.SetFloat("Blend", 1);
GotoNextPoint();
if (time == 99000 && animator.GetFloat("Blend") == 0)
{
time = 10000;
animator.SetFloat("Blend", 1);
//agent.isStopped = false;
GotoNextPoint();
}
}
else if (agent.remainingDistance <= 0.5f && agent.remainingDistance != 0f && time == 100000)
{
StartCoroutine(DoCheck());
animator.SetFloat("Blend",0);
GotoNextPoint();
}
else if(animator.GetFloat("Blend") == 0)
{
time--;
}
if (time == 99000 && animator.GetFloat("Blend") == 0)
{
time = 10000;
animator.SetFloat("Blend", 1);
//agent.isStopped = false;
agent.destination = points[destPoint].position;
destPoint = (destPoint + 1) % points.Length;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
NavMeshAgent not moving at given speed 0 Answers
Monsters are walking around without my permission, why? 0 Answers
Why won't my enemy follow my maze using NavMesh and Pathfinding? 0 Answers
Navmesh Agent isStopped not working, can't get navemesh agent to stop. 1 Answer
Blend Trees with NavMeshAgent 0 Answers