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:
using UnityEngine;
using System.Collections;
public class CharacterAgent : MonoBehaviour {
public NavMeshAgent agent;
public LookAt lookAt;
public Animator anim;
Vector2 smoothDeltaPosition = Vector2.zero;
Vector2 velocity = Vector2.zero;
public NavigationManager nm;
public CameraPerspectiveEditor cam;
private ICharacter character;
private bool display = true;
public Popup popup;
public Node currentNode;
public float speed = 0.5f;
// Use this for initialization
void Start () {
agent = GetComponent<NavMeshAgent>();
character = new Character("Dummy", 20);
transform.position = currentNode._position;
}
// Update is called once per frame
void Update () {
character.setCurrentNode(currentNode);
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
if (Physics.Raycast(cam.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity))
{
Debug.Log("Hit: " + hit.point);
currentNode.currentNode = false;
currentNode = nm.getNearestNode(hit.point);
currentNode.currentNode = true;
agent.SetDestination(currentNode._position);
}
}
}
void OnTriggerEnter(Collider c)
{
popup.ShowPopUp();
}
}
I am also using the script provided from the tutorial:
using UnityEngine;
using System.Collections;
public class AnimateMovement : MonoBehaviour {
public Animator anim;
NavMeshAgent agent;
Vector2 smoothDeltaPosition = Vector2.zero;
Vector2 velocity = Vector2.zero;
void Start()
{
agent = GetComponent<NavMeshAgent>();
agent.updatePosition = false;
}
void Update()
{
Vector3 worldDeltaPosition = agent.nextPosition - transform.position;
// Map 'worldDeltaPosition' to local space
float dx = Vector3.Dot(transform.right, worldDeltaPosition);
float dy = Vector3.Dot(transform.forward, worldDeltaPosition);
Vector2 deltaPosition = new Vector2(dx, dy);
// Low-pass filter the deltaMove
float smooth = Mathf.Min(1.0f, Time.deltaTime / 0.15f);
smoothDeltaPosition = Vector2.Lerp(smoothDeltaPosition, deltaPosition, smooth);
// Update velocity if delta time is safe
if (Time.deltaTime > 1e-5f)
velocity = smoothDeltaPosition / Time.deltaTime;
bool shouldMove = velocity.magnitude > 0.5f && agent.remainingDistance > agent.radius;
// Update animation parameters
anim.SetBool("move", shouldMove);
anim.SetFloat("velx", velocity.x);
anim.SetFloat("vely", velocity.y);
}
void OnAnimatorMove()
{
// Update postion to agent position
transform.position = agent.destination;
}
}
Thanks