I have a gameobject that uses navmesh to walk and when I want it to look at a gameobject when it gets within a radius it continues walking and also trys to look at the object at the same time which makes it spas out. How can I detect if the navmesh agent is done moving so that I can make the object look at another gameobject?
Hey there,
I am afraid I am having a bit of difficulty understanding what it is you are saying, from what I understand once the navmesh agent is within a certain range you want it to stop and find a new target. If that is the correct assumption, the solution is as follows (please note that not all of the following is in correct code format, and is instead a guide for you) :
private NavMeshAgent nma;
private Gamobject target;
Start(){
nma = transform.GetComponent<NavMeshAgent>();
StartCoroutine("Behavior1"); // I find for AI's the best practice is to use a series of co-routines as behaviors. run one untill a condition is met then transition
}
IEnumerator Behavior1(){
while (true){ // this way the behaviour will run untill you stop it manually
set destination to target
if (Vector3.Distance(transform.position, target.position) < radius){
StartCoroutine("NextBehavior"); starts next behavior, in your case finding the next target
break;// stops the current coroutine. this will stop the spazzing out
}
}
}
IEnumorator NextBehaviour(){
Whatever code selects a new target
target = new target
StartCoroutine("Behavior1");
I hope this helps ![]()
let me know if you need anything else.
this will allow you to detect if the object is moving. just change the animator stuff for your spas out stuff
var lastPosition : Vector3;
var myTransform : Transform;
var isMoving : boolean;
var anim : Animator;
function Start () {
myTransform = transform;
lastPosition = myTransform.position;
isMoving = false;
}
function Update ()
{
if ( myTransform.position != lastPosition )
anim.SetBool("moving",true);
else
anim.SetBool("moving",false);
lastPosition = myTransform.position;
}
OutOfRam thank you so much for your help. I also had no idea about agent.remaining distance so that should help me start the looking function at the right time. Thanks for explaining coroutines better so I can learn more about them too. Enjoy your sleep ![]()