- Home /
2 animations keep switching at a rapid pace
I am working on a personal prototype for a open world game and i am currently trying to make my first enemy ai. This is the way i have it set up:
The first second it spawns it records its spawning location into a vector3 var known as lastpos.
It stays at that location with an idle animation playing (moving = 0)
There is a sphere trigger around the enemy so when the player collides with the sphere it detects the player, it starts playing the running animation and starts chasing the playing (moving = 2)
when the player is not longer colliding with the sphere it walks back to lastpos while playing a walking animation (moving = 1)
when it goes back to its lastpos and the animation will go back to being idle.....or it should....
the problem i have with the last dot point is that my "moving" variable which is written to control the animations of the model keeps switching between 0 and 1 (or in other words the animations keep switching between idle and walking) and i have no idea whats going on. Its my first time designing an ai from scratch, if anyone could help me that would be great!
void Update () {
playerVector = player.transform.position;
gruntAnim.SetInteger ("moving", moving);
gruntAnim.SetInteger ("battle", inBattle);
moving = 0;
closeToPlayer = playerRange.GetComponent<combatDistanceToPlayer> ().enemyClose;
inBattle = 0;
if (playerDetected == true) {
nav.destination = player.transform.position;
inBattle = 1;
moving = 2;
if (closeToPlayer == true) {
moving = 0;
gruntAnim.SetBool ("attackablePlayer", true);
} else if (closeToPlayer == false) {
moving = 2;
gruntAnim.SetBool ("attackablePlayer", false);
}
//is the player currently detected and if he isnt then is the goblin back at the last position?
} else if (playerDetected == false & this.gameObject.transform.position != lastPos) {
nav.destination = lastPos;
inBattle = 0;
moving = 1;
}
if (this.gameObject.transform.position == lastPos) {
moving = 0;
}
}
actually i put in the debug.log command on this line of code
if (this.gameObject.transform.position == lastPos) {
moving = 0;
Debug.log("Goblin is back");
}
and nothing appears on the console. my prototype reacts as if that type of code is not there
okay its now been recognised but moving is still flicking between 0 and 1
Answer by webcam · Jun 28, 2017 at 04:48 PM
Not sure if this is the problem but it could because you are setting moving to 0 here
if (this.gameObject.transform.position == lastPos) {
moving = 0;
}
Try moving that statement into this check, because you only need to do this if the player is not detected
else if (playerDetected == false & this.gameObject.transform.position != lastPos)
{
nav.destination = lastPos;
inBattle = 0;
moving = 1;
if (this.gameObject.transform.position == lastPos) {
moving = 0;
}
}
actually i put in the debug.log command on that line of code and nothing appears on the console. my prototype reacts as if that type of code is not there
okay its now been recognised but its still flicking between 0 and 1