Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Error with navAgent.velocity

Discussion in 'Scripting' started by CaptCanada, Apr 24, 2019.

  1. CaptCanada

    CaptCanada

    Joined:
    Feb 3, 2013
    Posts:
    272
    Hi all

    I am getting an error message with this script, that I never got before when I used it in another project that has since been deleted.

    Here is the script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using BehaviorDesigner.Runtime;
    4. //using UnityEngine.AI.NavMeshAgent;
    5.  
    6.  
    7. public class MovementScript : MonoBehaviour
    8. {
    9.     //link to Animator component
    10.     public Animator animController;
    11.     //used to set anim controller parameters
    12.     public enum MoveState { Idle,Walking,Attack}
    13.     public MoveState moveState;
    14.     //link to NavMeshAgent component
    15.     public UnityEngine.AI.NavMeshAgent navAgent;
    16.     public BehaviorTree behaviorTree;
    17.     private bool canSeePlayer;
    18.     private bool canAttack;
    19.     public float damageAmount;
    20.  
    21.  
    22.     public void Start()
    23.  
    24.     {
    25.         canSeePlayer = ((SharedBool)behaviorTree.GetVariable("Chase")).Value;
    26.         canAttack = ((SharedBool)behaviorTree.GetVariable("Attack")).Value;          
    27.      
    28.     }
    29.    
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.         //character walks if there is a navigation path set, idle all other times
    34.         canSeePlayer = ((SharedBool)behaviorTree.GetVariable("Chase")).Value;
    35.         canAttack = ((SharedBool)behaviorTree.GetVariable("Attack")).Value;
    36.  
    37.  
    38.  
    39.         if (canSeePlayer)
    40.  
    41.         {
    42.             moveState = MoveState.Walking;
    43.             print(moveState);
    44.             print("I see you!");
    45.  
    46.             if (canAttack)
    47.             {
    48.                 print("Attacking you!");
    49.                 moveState = MoveState.Attack;
    50.  
    51.             }
    52.  
    53.  
    54.         }
    55.         else
    56.         {
    57.             moveState = MoveState.Walking;
    58.         }
    59.              
    60.         //send move state info to animator controller
    61.         animController.SetInteger("MoveState", (int)moveState);
    62.  
    63.     }
    64.     void OnAnimatorMove()
    65.     {
    66.         //only perform if walking
    67.         if (moveState == MoveState.Walking)
    68.         {
    69.             print("In OnAnimatorMove Function");
    70.             //set the navAgent's velocity to the velocity of the animation clip currently playing
    71.             navAgent.velocity = animController.deltaPosition / Time.deltaTime;
    72.          
    73.             //smoothly rotate the character in the desired direction of motion
    74.            Quaternion lookRotation = Quaternion.LookRotation(navAgent.desiredVelocity);
    75.            transform.rotation = Quaternion.RotateTowards(transform.rotation, lookRotation, navAgent.angularSpeed * Time.deltaTime);
    76.         }
    77.     }
    78.  
    79.    
    80. }
    81.  
    Here is the error that I am getting,

    navmeshagent.velocity assign attempt for 'creature1' is not valid. Input velocity is { NaN, NaN, NaN }.
    UnityEngine.AI.NavMeshAgent:set_velocity(Vector3)
    MovementScript:OnAnimatorMove() (at Assets/Scipts/MovementScript.cs:71)

    I am not sure why I am getting this and I am a basic C# coder, so I am not sure what NaN is, though I found in the Unity Answers it refers to Not A Number?

    If I comment the navAgent.velocity line the code runs and my Agent moves but very quickly and I don't get the error.

    When I used this script in another project, I am sure I didn't get this error.

    This script is used with Behavior Designer in order to move an Agent across a NavMesh and to control which animation is played based on the Agent seeing the player and attack the player.

    Thanks for any help
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Maybe Time.deltaTime is 0, either through Time.scale being 0 or some other event, such that you're dividing by zero resulting in NaN. That's my only guess.