- Home /
NavMesh Agents: They won't stop when you want them X units away from the target and 2 other questions...
I'm getting some weird behaviour from the NavMesh pathfinding that comes with Unity. Yes, I know there are a lot of other options out there including RAIN and other AI solutions that are "better", but I'm trying to keep costs low. I'm using Unity 5.1.3f1 because 5.2.x branch has some bugs with Mechanim and other subsystems (I haven't checked the release notes to see if they have been fixed).
Here's the script I'm using, which automatically assigns a NavMeshAgent item to the object you wish to control:
using UnityEngine;
using System.Collections;
using System;
[RequireComponent(typeof(NavMeshAgent))]
public class NPCFollowsEntity : MonoBehaviour
{
public GameObject targetEntity;
[Range(0.5f, 2.0f)]
public float closeEnough = 1.0f;
public bool controlAnimations = true; // if true, we'll pass the speed into the character's animation controller.
private NavMeshAgent _navAgent; // our working reference to the navagent
private Animator _ac; // our working reference to the animation controller
private bool moveTowardsEntity = true; // an attempt to try to get the AI to only move if this was set, but alas it was borked
// Use this for initialization
void Start()
{
// Give them the satnav.
_navAgent = gameObject.GetComponent<NavMeshAgent>();
if (!_navAgent)
{
Debug.LogError("NPC Error: No navmesh agent. Abort.");
this.enabled = false;
return;
}
// If we are to control animations, and they haven't set the animator...
if (controlAnimations)
{
_ac = gameObject.GetComponentInChildren<Animator>();
if (!_ac)
{
Debug.LogError("NPC Error: No animator yet we want to control animations?");
controlAnimations = false;
}
}
}
//
void Update()
{
// an attempt to make the AI look at the target entity which was kinda iffy.
// transform.rotation.SetLookRotation(targetEntity.transform.position, transform.up);
}
void LateUpdate()
{
if (controlAnimations) UpdateAnimationController();
CheckNPCTargetStatus();
}
void FixedUpdate()
{
/* The following is a failed attempt at shooting a ray to see if the entity we hit is close enough.
RaycastHit distanceToEntity;
if(Physics.Raycast(transform.position, transform.forward, out distanceToEntity))
{
if (distanceToEntity.distance <= closeEnough) moveTowardsEntity = false;
else moveTowardsEntity = true;
} */
}
private void UpdateAnimationController()
{
_ac.SetFloat("speed", _navAgent.velocity.magnitude); // doesn't work... well it does but it derps around
}
private void CheckNPCTargetStatus()
{
if (moveTowardsEntity) _navAgent.destination = targetEntity.transform.position;
// else _navAgent.destination = transform.position; <- ignore this
if (_navAgent.stoppingDistance != closeEnough) _navAgent.stoppingDistance = closeEnough;
}
}
If you copy and paste that C# script into your project, make a test level, add a capsule as the target, and set the parameters like this:

And you should get something like this video located here (228KB). The video shows the AI NavAgent "bouncing" or "rubber banding" between like half a unit and the actual player. Applying Root Motion or not doesn't affect the outcome, it always "bounces" like this. Stopping Distance seems to do nothing constructive. Is this a bug?
My question here is what am I doing wrong in the code? Do I need to set the AI destination once and once only, and instead at Start() rather than Update() ?
Secondly, how would I make it so that the NavMesh Agent-controlled entity always looked at the target? As you can see in LateUpdate() I tried to set the look position using SetLookRotation... but I'm not sure that is what I want. Basically, the AI should look at the target entity (in this case, the player) so it doesn't look like the AI is just moon walking.
Last but not least, say for example the player has died, and AIs no longer should follow that target. If I set the target to null, will the AI stop? Or is there a method I can call that will stop the AI? (I guess another way is just to set another position and let the AI chase after that...)
If you manage to get it working, please let me know what you changed. Cheers!
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Distribute terrain in zones 3 Answers
NavAgent not following on rebuilt NavMesh 0 Answers
Multiple Cars not working 1 Answer