I have started converting the following script to use mechanim with the AI script component.
https://docs.unity3d.com/Manual/nav-CouplingAnimationAndNavigation.html
However, at the start of the example script, they use:
void Start ()
{
anim = GetComponent<Animator> ();
agent = GetComponent<NavMeshAgent> ();
// Don’t update position automatically
agent.updatePosition = false;
}
Is there any similar way to set updatePosition to false for the AI script component? If not, can you suggest which part of the script I should be looking at?
Currently my script looks like this, with some strange results (I am guessing because the agent position is being updated by the AI script).
using UnityEngine;
using Pathfinding;
[RequireComponent (typeof (Pathfinding.IAstarAI))]
[RequireComponent (typeof (Animator))]
public class LocomotionSimpleAgent : MonoBehaviour {
Animator anim;
IAstarAI aiAgent;
public Vector2 smoothDeltaPosition = Vector2.zero;
public Vector2 smoothDeltaPositionStart = Vector2.zero;
public Vector2 velocity = Vector2.zero;
public bool shouldMove;
public Vector3 worldDeltaPosition;
public float dy;
public float dx;
public Vector2 deltaPosition;
public float smooth;
public float agentRadius = 0.5f;
public Vector3 destination = new Vector3(0,0,0);
void Start () {
anim = GetComponent<Animator> ();
aiAgent = GetComponent<IAstarAI>();
aiAgent.isStopped = true;
}
void Update () {
// Check if agent has path, or the return will be inft.
if(!aiAgent.hasPath)
{
destination = new Vector3(0,0,0);
}
else
{
destination = aiAgent.destination;
}
//Vector3 worldDeltaPosition = agent.nextPosition - transform.position;
worldDeltaPosition = destination - transform.position;
// Map 'worldDeltaPosition' to local space
dx = Vector3.Dot (transform.right, worldDeltaPosition);
dy = Vector3.Dot (transform.forward, worldDeltaPosition);
deltaPosition = new Vector2 (dx, dy);
// Low-pass filter the deltaMove
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;
// Velocity from AI Agent
velocity = aiAgent.velocity;
// Manually set agent radius.
shouldMove = velocity.magnitude > 0.5f && aiAgent.remainingDistance > agentRadius;
// Update animation parameters
anim.SetBool("move", shouldMove);
anim.SetFloat ("velx", velocity.x);
anim.SetFloat ("vely", velocity.y);
}
void OnAnimatorMove () {
// Update position based on animation movement using navigation surface height
Vector3 position = anim.rootPosition;
position.y = destination.y;
transform.position = position;
}
}