Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

NavMesh and Mecanim

Discussion in 'Scripting' started by MohammadM, Jun 12, 2014.

  1. MohammadM

    MohammadM

    Joined:
    Jun 8, 2014
    Posts:
    28
    Hello.
    I am trying to make a character move using navigation mesh and animated it using Mecanim at the same time. The problem is , The character is moving and reaching a target but the animations wont play for some reason. any one got a solution for this?

    Thanks.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    You're going to have to tell us how you are triggering the mechanim states and transitions for us to help...
     
    MohammadM likes this.
  3. MohammadM

    MohammadM

    Joined:
    Jun 8, 2014
    Posts:
    28
    Ur question gave me an idea to what was going on and i solved it, Thank you v much :).
    The problem was that the animation never moves from ideal to walk and so on. so i figured maybe the speed variable is not working for some reason. This is how my animator look.

    And here is my navmesh script

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MoveManager : MonoBehaviour {
    5.  
    6.     public Transform target;
    7.     Animator anim;
    8.     NavMeshAgent agent;
    9.     void Start ()
    10.     {
    11.         agent = GetComponent<NavMeshAgent> ();
    12.         anim = GetComponent<Animator> ();
    13.         if(anim.layerCount ==2)
    14.             anim.SetLayerWeight(1, 1);
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update ()
    19.     {
    20.  
    21.         agent.SetDestination (target.position);
    22.  
    23.     }
    24. }
    25.  
    so I am walking tell i reach a target but i am stuck on the ideal state . I thought when the character was moving . the Speed variable will change. But it did not. Its strange because if i am the one controlling the character it does change without me touching it .

    So i did this now , and the walking animation did play.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MoveManager : MonoBehaviour {
    5.  
    6.     public Transform target;
    7.     Animator anim;
    8.     NavMeshAgent agent;
    9.     void Start ()
    10.     {
    11.         agent = GetComponent<NavMeshAgent> ();
    12.         anim = GetComponent<Animator> ();
    13.         if(anim.layerCount ==2)
    14.             anim.SetLayerWeight(1, 1);
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update ()
    19.     {
    20.         agent.SetDestination (target.position);
    21.         if(transform.position!=target.position)
    22.         {
    23.             anim.SetFloat ("Speed", 0.2f);
    24.             Debug.Log("Me"+transform.position);
    25.             Debug.Log("Target"+target.position);
    26.         }
    27.  
    28.             if(transform.position==target.position)
    29.             {
    30.             anim.SetFloat ("Speed", 0.0f);
    31.             Debug.Log("Hello");
    32.             }
    33.            
    34.  
    35.  
    36.  
    37.  
    38.     }
    39. }
    40.  
    The Walk animation works now, but when i reach the target and set the Speed back to zero to go back to the ideal state. its not playing the ideal animation, the Speed is not going back to zero. Actually the second if statement is not working.
     
  4. MohammadM

    MohammadM

    Joined:
    Jun 8, 2014
    Posts:
    28
    Ok i got it working...Not sure How.

    I changed the if Statement to look like this.
    Code (CSharp):
    1. if(agent.transform.position.x!=target.transform.position.x)
    2.         {
    3.             anim.SetFloat ("Speed", 0.2f);
    4.             Debug.Log("Me"+agent.transform.position);
    5.             Debug.Log("Target"+target.transform.position);
    6.         }
    7.  
    8.         if(agent.transform.position.x==target.transform.position.x)
    9.             {
    10.             anim.SetFloat ("Speed", 0.0f);
    11.             Debug.Log("Hello");
    12.             }
    I thought maybe comparing 2 3D vectors is not working, so i changed to compare the X position only. and that worked.. Not sure why tho. would love if some one can explain it ;) sorry for the long post. And Thanks a lot for helping.
     
    Last edited: Jun 12, 2014
  5. Crazydadz

    Crazydadz

    Joined:
    Mar 29, 2012
    Posts:
    50
    agent.hasPath will tell you if your agent is trying to reach a target or not. I'm using this to detect if my agent has reach the target position or not. agent.hasPath = true means : "I'm on my way to the destination", false means : "I'm don't have any destination so I'm not moving". You could also use agent.desiredVelocity to set your animation Speed. I just begin to play with the navmesh, so there's is maybe better way :).

    Code (CSharp):
    1. agent.hasPath agent.desiredVelocity
     
    MohammadM likes this.
  6. MohammadM

    MohammadM

    Joined:
    Jun 8, 2014
    Posts:
    28
    Thanks for the help :).