- Home /
Animations not Transitioning
I have a monster in my game that is supposed to free roam around the map, and when the player gets around 20f from it the monster will turn to look at the player and run its idle animation; once the player gets within 10f the monster runs at the player and when it gets right beside the player it stops and runs its attack animation. I had everything working right on Sunday, but when I booted up Unity on Monday the animations wouldn't transition from the default walking animation. The only thing I had changed in that time was add a flashlight script to the player character, which I wouldn't think would affect the monsters (all the script does is turn a directional light pointing from the player on and off). I'm using bools to transition between animations. Any advice on this would help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class enemyAITest : MonoBehaviour
{
public Transform player; //target
public float playerDistance; //determines how far from target before action takes place
public float rotationDamping; //determines how quickly rotation occurs
public float chaseSpeed; //determines how quickly to pursue target
public float wanderSpeed; //determines how quickly to move around map
public float maxDist;
static Animator anim;
Vector3 wayPoint;
// Use this for initialization
void Awake()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update ()
{
transform.position += transform.TransformDirection(Vector3.forward) * wanderSpeed * Time.deltaTime;
playerDistance = Vector3.Distance(player.position, transform.position);
while ((transform.position - wayPoint).magnitude < 3)
Wander();
if (playerDistance < 20f)
{
LookAtPlayer();
wanderSpeed = 0;
anim.SetBool("isWalking", false);
anim.SetBool("isIdle", true);
}
if (playerDistance < 10f)
{
Chase();
anim.SetBool("isIdle", false);
anim.SetBool("isWalking", false);
anim.SetBool("isRunning", true);
}
if(playerDistance < 1f)
{
chaseSpeed = 0;
anim.SetBool("isRunning", false);
anim.SetBool("isAttacking", true);
}
else
anim.SetBool("isWalking", true);
anim.SetBool("isIdle", false);
}
void LookAtPlayer()
{
Quaternion rotation = Quaternion.LookRotation(player.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationDamping);
wanderSpeed = 0;
}
void Chase()
{
Vector3 direction = player.position - transform.position;
direction.y = 0;
transform.Translate(Vector3.forward * chaseSpeed * Time.deltaTime);
}
void Wander()
{
Vector3 MonsterPosition = new Vector3(Random.Range(transform.position.x - maxDist, transform.position.x + maxDist), 1, Random.Range(transform.position.z - maxDist, transform.position.z + maxDist));
wayPoint.y = 1;
transform.LookAt(wayPoint);
//Debug.Log(wayPoint + " and " + (transform.position - wayPoint).magnitude);
}
}
In case it would help, here's a screenshot of my animator (done through Unity):