- Home /
navAgent.velocity input returns NaN
Hi all
Here is a script I have attached to a navAgent to navigate a mesh. using UnityEngine; using System.Collections; using BehaviorDesigner.Runtime; using UnityEngine.AI;
public class MovementScript : MonoBehaviour
{
//link to Animator component
public Animator animController;
//used to set anim controller parameters
public enum MoveState { Idle,Walking,Attack}
public MoveState moveState;
//link to NavMeshAgent component
public NavMeshAgent navAgent;
public BehaviorTree behaviorTree;
private bool canSeePlayer;
private bool canAttack;
public float damageAmount;
public void Start()
{
canSeePlayer = ((SharedBool)behaviorTree.GetVariable("Chase")).Value;
canAttack = ((SharedBool)behaviorTree.GetVariable("Attack")).Value;
}
// Update is called once per frame
void Update()
{
//character walks if there is a navigation path set, idle all other times
canSeePlayer = ((SharedBool)behaviorTree.GetVariable("Chase")).Value;
canAttack = ((SharedBool)behaviorTree.GetVariable("Attack")).Value;
if (canSeePlayer)
{
moveState = MoveState.Walking;
print(moveState);
print("I see you!");
if (canAttack)
{
print("Attacking you!");
moveState = MoveState.Attack;
}
}
else
{
moveState = MoveState.Walking;
}
//send move state info to animator controller
animController.SetInteger("MoveState", (int)moveState);
}
void OnAnimatorMove()
{
navAgent.updateRotation = false;
//only perform if walking
if (moveState == MoveState.Walking)
{
print("In OnAnimatorMove Function");
print(navAgent.velocity);
//set the navAgent's velocity to the velocity of the animation clip currently playing
navAgent.velocity = animController.deltaPosition / Time.deltaTime;
//smoothly rotate the character in the desired direction of motion
Quaternion lookRotation = Quaternion.LookRotation(navAgent.desiredVelocity);
transform.rotation = Quaternion.RotateTowards(transform.rotation, lookRotation, navAgent.angularSpeed * Time.deltaTime);
}
}
}
I am getting an error in the navAgent.velocity=animController.deltaPosition/Time.deltaTime; line saying that the navAgent.velocity is NaN
I am not sure what is causing this and need some help.
Thanks
Animator.applyRoot$$anonymous$$otion must be enabled for deltaPosition to be calculated
Are you applying root motion with your Animator? If not, that is probably your NaN.
On line 68ish you are dividing the velocity by deltaTime. Dividing by 0 will give nan. Save the assignment to a variable first, check if it is 0. Don't know why deltatime would be 0, but it's the only nan source i can find.
Answer by CaptCanada · Apr 30, 2019 at 02:13 PM
I read the docs in your link and it mentions that when using OnAnimatorMove(), that Animator.applyRootMotion won't have an effect.
I am using OnAnimatorMove() in my script.
Your answer
