Combining State Machine with Pathfinding
Hi, I have a wandering chicken, but I can't seem to stop her randomly moving while in idle, and then also walking on the spot.
Can anyone see the issue with my code?
using UnityEngine;
using System.Collections;
public class ChickenAi : MonoBehaviour {
public Transform[] wayPoints;
NavMeshAgent navMesh;
int randomNumber;
public int behaviour;
public Transform currentWaypoint;
Animator anim;
// Use this for initialization
void Start () {
navMesh=GetComponent<NavMeshAgent>();
anim=GetComponent<Animator>();
StartCoroutine(AnimalBehaviour());
}
IEnumerator AnimalBehaviour(){
while (true) {
//Choose a behaviour
behaviour = Random.Range (0, 4);
if (behaviour == 0) {
//Choose a random waypoint
randomNumber=Random.Range(0,wayPoints.Length);
currentWaypoint=wayPoints[randomNumber];
//go to selected waypoint
navMesh.SetDestination (currentWaypoint.position);
//Apply animation
anim.SetTrigger("Walk");
}
else if (behaviour == 1) {
anim.SetTrigger("Idle1");
//stop Movement
navMesh.ResetPath();
}
else if (behaviour == 2) {
anim.SetTrigger("Idle2");
//stop Movement
navMesh.ResetPath();
}
else if (behaviour == 3) {
anim.SetTrigger("Eat");
Debug.Log ("Eating");
//stop Movement
navMesh.ResetPath();
}
yield return new WaitForSeconds(Random.Range(5.0f,6.0f));
}
}
}
I've tried using NavMesh.Stop() and navMesh.Resume() but still getting the mismatched behaviours.
Thanks
Answer by _Game_Dev_Dude_ · Nov 19, 2015 at 03:25 PM
Code looks correct after a quick glance. I would make sure your animation's don't move the chicken. This has to deal with some animation settings and how it was animated.
Thanks for that feedback.. Do you think it could be my nav$$anonymous$$eshAgent settings or state machine settings? Every transition in the statemachine is set up like this. None of the animations have root motion.
I'm pondering the "Broken state machine" theory but Its probably user error..