Apologies for making yet another thread on NavMeshAgents but believe me, I have looked at most topics reading each one of them over and over and none of the solutions I’ve found were able to help me out. Yes I am a noob, maybe not to scripting but a big yes to Unity and Java.
Well the deal is, my Agent slides or rather moves for apparently a fixed distance before it stops. I have tried several NavMeshAgent methods, Stop(), enable(), ResetPath(), SetPath() to itself but it keeps moving towards the target or just straight if I ResetPath(). I am suspicious that this is not something with my code, maybe something with one of the NavMeshAgent’s variables even thoughI have also changed accelaration et al to several super-small or large values?
If any of you would be kind enough to give me pointers it would be immensely appreciated. It may well be something silly and I will embarrass myself yet again on a different forum.
var Distance;
var target : Transform;
var searchDistance = 45.0;
var interestDistance = 30.0;
var chaseRange = 15.0;
var Damping = 6.0; //smooths rotation
private var navMeshComponent: NavMeshAgent;
function Start ()
{
navMeshComponent = gameObject.GetComponent(NavMeshAgent);
}
function Update ()
{
Distance = Vector3.Distance(target.position, transform.position);
if (Distance > searchDistance)
{
animation.Play("LookAround01");
}
if (Distance < searchDistance Distance > interestDistance)
{
navMeshComponent.ResetPath(); // it does indeed reset the path, it stops following
navMeshComponent.SetDestination(transform.position); // keeps sliding
navMeshComponent.Stop(true); // keeps sliding
search();
}
if (Distance < interestDistance Distance > chaseRange)
{
investigate();
}
if (Distance < chaseRange)
{
chase();
}
}
//FUNCTIONS
function search ()
{
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
animation.Play("LookAround01");
}
function investigate ()
{
navMeshComponent.destination = target.position;
navMeshComponent.speed = 3.0;
animation.CrossFade("AngryWalk_AngryWalk");
}
function chase ()
{
navMeshComponent.destination = target.position;
navMeshComponent.speed = 7.0;
animation.CrossFade("Running01");
}
Thank you for you reply James, yes I’ve tried that by setting:
navMeshComponent.updatePosition = false;
And the agent still moves towards me, it shifts to the “LookAround01” animation in the search() function when I reach the specified distance but it keeps moving towards me using this animation which actually looks like if he’s ice skating but to be honest this stopped being funny a few hours ago.
I have no idea what is going on. My acceleration value is set to 80, played with other settings just to see if the agent was taking its time before stopping but I am yet to be successful on this.
Nope, no other scripts. It seems that nothing within this if clause is working:
if (Distance < searchDistance Distance > interestDistance)
{
navMeshComponent.ResetPath(); // it does indeed reset the path, it stops following
navMeshComponent.SetDestination(transform.position); // keeps sliding
navMeshComponent.Stop(true); // keeps sliding
search();
}
Maybe I’m too tired to spot something wrong here? I have no idea why it isn’t calling ‘search()’. I’ve disabled my if clause that plays ‘LookAround01’ and now I see that the animation is not being called on the search() nor if I place the animation in the if clause above.
Thanks Sajid, I have tried .enable and I have read your topic many times. It was something with my if clause and I just can’t figure it out. I have eliminated the if clause and worked around it and now I am getting the expected behavior. Not ideal I have to say but at least I can move forward. Thanks to you and James for taking the time to reply to this thread.
Sure, but I really haven’t done much to be honest. I have eliminated one of my if clauses and used the .Stop and .Resume functions which were not working within the eliminated if clause for some obscure reason, I’ve used .Stop(true) when the player gets too far and the .Resume() in the investigate() function called when the player gets closer.
Here it is:
var Distance;
var target : Transform;
var searchDistance = 45;
var interestDistance = 30;
var chaseRange = 15;
var Damping = 6.0; //smooths rotation
private var navMeshComponent: NavMeshAgent;
function Start ()
{
navMeshComponent = gameObject.GetComponent(NavMeshAgent);
}
function Update ()
{
Distance = Vector3.Distance(target.position, transform.position);
if (Distance > interestDistance)
{
animation.CrossFade("LookAround01");
navMeshComponent.Stop(true);
}
if (Distance <= interestDistance Distance >= chaseRange)
{
investigate();
}
if (Distance < chaseRange)
{
chase();
}
}
//FUNCTIONS
function investigate ()
{
navMeshComponent.Resume();
navMeshComponent.destination = target.position;
navMeshComponent.speed = 3.0;
animation.CrossFade("AngryWalk_AngryWalk");
}
function chase ()
{
navMeshComponent.destination = target.position;
navMeshComponent.speed = 7.0;
animation.CrossFade("Running01");
}