- Home /
Add animation to a NavMeshAgent Character
Hi, Im working on a character controller where the player clicks somewhere in the world and the character moves to that point. I have created a nav mesh agent that follows a target, the target moves to where clicked using the "click to move" then the nav mesh agent moves to that location using a follow script which is great. Only problem I can't solve is how to animate this nav mesh agent? Also my agent moves backwards which is frustrating but i expect is a pivot problem.
Ive got this script but it doesnt like the .speed command, where have i gone wrong? (JavaScript)
function Start ()
{
// By default loop all animations
animation.wrapMode = WrapMode.Loop;
}
function Update ()
{
var currentSpeed = NavMeshAgent.speed;
// Switch between idle and walk
if (currentSpeed > 0.1)
animation.CrossFade("Walk");
else
animation.CrossFade("idle1");
}
Answer by J Lam · Jul 17, 2012 at 12:16 PM
var lastFramePosition: Vector3;
function Start (){
// By default loop all animations
animation.wrapMode = WrapMode.Loop;
lastFramePosition = transform.position;
}
function Update ()
{
var currentFramePosition : Vector3 = transform.position;
var distance : float = Vector3.Distance(lastFramePosition, currentFramePosition);
lastFramePosition = currentFramePosition;
var currentSpeed: float = Mathf.Abs(distance)/Time.deltaTime;
// Switch between idle and walk
if (currentSpeed > 0.1)
animation.CrossFade("Walk");
else
animation.CrossFade("idle1");
}
You may try the above script and see if it can help. I think calculating its own speed might be a better way if your nav mesh agent already able to follow the character.