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

Nav Anim Trouble

Discussion in 'Navigation' started by ExOblivione, Aug 15, 2015.

  1. ExOblivione

    ExOblivione

    Joined:
    Jul 7, 2014
    Posts:
    8
    I am trying to use root motion to drive my enemy to a target location. The enemy does go to the nav destination but does not avoid any obstacles. You see in my code from all the commented out lines I've tried many things (probably incorrectly). This is my 1st attempt at coupling the nav and anim. I've followed the Stealth Tut and did the "Coupling Animation and Navigation" in the manual. But I just cant seem to get it to work.

    Ultimately I want to spawn enemies and set a speed for them to move towards the Player. Each wave I will increase movement speed independently of the other animations on the animator.

    Code (JavaScript):
    1.  
    2. var TargetObj : Transform;
    3.  
    4. private var avatar : Animator;
    5. private var SpeedDampTime : float = .25f;
    6. private var DirectionDampTime : float  = .25f;
    7. private var rb : Rigidbody;
    8. private var agent : NavMeshAgent;
    9. private var curentDir : Vector3;
    10. private var wantedDir : Vector3;
    11.  
    12.     function Start (){
    13.  
    14.         rb = GetComponent(Rigidbody);
    15.      
    16.         avatar = GetComponent(Animator);
    17.      
    18.  
    19.         avatar.speed = speed + UnityEngine.Random.Range(-0.4f, 0.4f);
    20.      
    21.         agent = GetComponent(NavMeshAgent);
    22.         agent.updatePosition = false;
    23.         agent.updateRotation = false;
    24.  
    25.     }
    26.  
    27.     function FixedUpdate (){
    28.  
    29.  
    30.  
    31.         if (avatar && TargetObj)
    32.         {        
    33.             if(Vector3.Distance(TargetObj.position,avatar.rootPosition) > 4)
    34.             {
    35.                 avatar.SetFloat("Speed",1,SpeedDampTime, Time.deltaTime);
    36.          
    37.                 curentDir = avatar.rootRotation * Vector3.forward;
    38.                 wantedDir = (TargetObj.position - avatar.rootPosition).normalized;
    39.  
    40.                 if(Vector3.Dot(curentDir,wantedDir) > 0)
    41.                 {
    42.                     avatar.SetFloat("Angle",Vector3.Cross(curentDir,wantedDir).y,DirectionDampTime, Time.deltaTime);
    43.                 }
    44.                 else
    45.                 {
    46.                     avatar.SetFloat("Angle", Vector3.Cross(curentDir,wantedDir).y > 0 ? 1 : -1, DirectionDampTime, Time.deltaTime);
    47.                 }
    48.             }
    49.             else
    50.             {
    51.                 avatar.SetFloat("Speed",0,SpeedDampTime, Time.deltaTime);
    52.             }
    53.         }    
    54.     //rb.velocity = agent.velocity;
    55.     //agent.nextPosition = rb.position;
    56.  
    57.     }
    58.  
    59.     function OnAnimatorMove(){
    60.  
    61. //controller.Move(avatar.deltaPosition);
    62. //transform.Translate(avatar.deltaPosition);
    63. //transform.rotation = avatar.rootRotation;
    64. var anim = GetComponent(Animator);
    65. var delta : Vector3;
    66. delta = this.GetComponent(Animator).deltaPosition;
    67. //var newPosition = transform.position;
    68.  
    69. //newPosition.z += anim.GetFloat("Speed") * Time.deltaTime;                              
    70.  
    71. //transform.position = newPosition;
    72. //Debug.Log("delta : " + delta);
    73. //transform.LookAt(transform.position + agent.desiredVelocity);
    74. transform.position = anim.rootPosition;
    75. transform.rotation = anim.rootRotation;
    76. //transform.position = agent.nextPosition;
    77. //agent.velocity = anim.deltaPosition / Time.deltaTime;
    78.     }
    Any suggestions on how to fix or at least improve would be appreciated. I think I way be over-killing it a bit...
     
    Last edited: Aug 20, 2015