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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

NavAgent and Animations

Discussion in 'Animation' started by sk8terboy4, Feb 13, 2016.

  1. sk8terboy4

    sk8terboy4

    Joined:
    Mar 29, 2013
    Posts:
    29
    I'm trying to add animations to my node based movement. It is mixed with the navigation system. I want my player to move to the node (agent sets the destination to that node) while playing animations. I used these scripts and setup in the tutorial here: http://docs.unity3d.com/Manual/nav-CouplingAnimationAndNavigation.html

    Here is my agent script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CharacterAgent : MonoBehaviour {
    5.  
    6.     public NavMeshAgent agent;
    7.     public LookAt lookAt;
    8.     public Animator anim;
    9.     Vector2 smoothDeltaPosition = Vector2.zero;
    10.     Vector2 velocity = Vector2.zero;
    11.     public NavigationManager nm;
    12.     public CameraPerspectiveEditor cam;
    13.  
    14.     private ICharacter character;
    15.     private bool display = true;
    16.     public Popup popup;
    17.     public Node currentNode;
    18.     public float speed = 0.5f;
    19.  
    20.     // Use this for initialization
    21.     void Start () {
    22.         agent = GetComponent<NavMeshAgent>();
    23.  
    24.         character = new Character("Dummy", 20);
    25.  
    26.         transform.position = currentNode._position;
    27.     }
    28.    
    29.     // Update is called once per frame
    30.     void Update () {
    31.         character.setCurrentNode(currentNode);
    32.  
    33.         if (Input.GetMouseButtonDown(0))
    34.         {
    35.             RaycastHit hit;
    36.             if (Physics.Raycast(cam.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity))
    37.             {
    38.                 Debug.Log("Hit: " + hit.point);
    39.                 currentNode.currentNode = false;
    40.                 currentNode = nm.getNearestNode(hit.point);
    41.                 currentNode.currentNode = true;
    42.                 agent.SetDestination(currentNode._position);
    43.             }
    44.         }
    45.     }
    46.     void OnTriggerEnter(Collider c)
    47.     {
    48.         popup.ShowPopUp();
    49.     }
    50.  
    51. }
    52.  
    I am also using the script provided from the tutorial:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AnimateMovement : MonoBehaviour {
    5.  
    6.    public Animator anim;
    7.     NavMeshAgent agent;
    8.     Vector2 smoothDeltaPosition = Vector2.zero;
    9.     Vector2 velocity = Vector2.zero;
    10.  
    11.     void Start()
    12.     {
    13.         agent = GetComponent<NavMeshAgent>();
    14.         agent.updatePosition = false;
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         Vector3 worldDeltaPosition = agent.nextPosition - transform.position;
    20.  
    21.         // Map 'worldDeltaPosition' to local space
    22.         float dx = Vector3.Dot(transform.right, worldDeltaPosition);
    23.         float dy = Vector3.Dot(transform.forward, worldDeltaPosition);
    24.         Vector2 deltaPosition = new Vector2(dx, dy);
    25.  
    26.         // Low-pass filter the deltaMove
    27.         float smooth = Mathf.Min(1.0f, Time.deltaTime / 0.15f);
    28.         smoothDeltaPosition = Vector2.Lerp(smoothDeltaPosition, deltaPosition, smooth);
    29.  
    30.         // Update velocity if delta time is safe
    31.         if (Time.deltaTime > 1e-5f)
    32.             velocity = smoothDeltaPosition / Time.deltaTime;
    33.  
    34.         bool shouldMove = velocity.magnitude > 0.5f && agent.remainingDistance > agent.radius;
    35.  
    36.         // Update animation parameters
    37.         anim.SetBool("move", shouldMove);
    38.         anim.SetFloat("velx", velocity.x);
    39.         anim.SetFloat("vely", velocity.y);
    40.  
    41.     }
    42.  
    43.     void OnAnimatorMove()
    44.     {
    45.         // Update postion to agent position
    46.         transform.position = agent.destination;
    47.     }
    48. }
    49.  
    Thanks
     

    Attached Files:

  2. sk8terboy4

    sk8terboy4

    Joined:
    Mar 29, 2013
    Posts:
    29
    Never mind I fixed the problem