- Home /
Movement and Animation state conflict
I can get a gameObject to cycle through some basic animations created with Unity's animator. In a separate scene without those basic animation states, I can get that same gameObject to move somewhat randomly around a field using a script. However, when I try to combine the two -- having the object cycle through the animation states WHILE wandering around the field -- the animation seems to cancel out the movement script.
Is there a general guideline regarding how to avoid conflicts like this?
Here is the code for the movement script courtesy of 'NFA78' [https://www.youtube.com/watch?v=Oqajv3vL58k]
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class randomMovement: MonoBehaviour {
UnityEngine.AI.NavMeshAgent navMeshAgent;
public float timerForNewPath;
private bool inCoroutine;
// Start is called before the first frame update
void Start()
{
navMeshAgent = GetComponent<UnityEngine.AI.NavMeshAgent>();
}
Vector3 getNewRandomPosition()
{
float x = Random.Range(-40, 40);
float z = Random.Range(-40, 40);
Vector3 pos = new Vector3(x, 0, z);
return pos;
}
IEnumerator doSomething()
{
inCoroutine = true;
yield return new WaitForSeconds(timerForNewPath);
GetNewPath();
inCoroutine = false;
}
void GetNewPath()
{
navMeshAgent.SetDestination(getNewRandomPosition());
}
// Update is called once per frame
void Update()
{
if (!inCoroutine)
{
StartCoroutine(doSomething());
}
} }
Answer by Siggytron · Feb 13, 2019 at 10:47 PM
It appears a partial solution to this problem is to make sure the 'Apply Root Motion' box is ticked in the Animator attached to the gameObject.