I followed this tutorial of making enemy ai:
But when I try to clone the object, the clone does not switch animation state. But when I delete the original and leave only the clone, it works. Why is it only one works at a time? The movement works, just not the animation transitions. Am I doing something wrong?
Here's the code:
using UnityEngine;
using System.Collections;
public class EnemyBehaviorScript : MonoBehaviour {
public Transform Player;
public float LookSpeed;
public float MovementSpeed;
public float LookDistance;
public float AttackDistance;
static Animator anim;
Vector3 direction;
void Start ()
{
anim = GetComponent ();
}
void Update ()
{
direction = Player.position - this.transform.position;
direction.y = 0;
if (Vector3.Distance (Player.position, this.transform.position) < LookDistance)
{
this.transform.rotation = Quaternion.Slerp (this.transform.rotation, Quaternion.LookRotation (direction), LookSpeed);
if (direction.magnitude > AttackDistance - 0.5f) {
anim.SetBool ("isWalking", true);
anim.SetBool ("isAttacking", false);
anim.SetBool ("isIdle", false);
this.transform.Translate (0, 0, MovementSpeed*Time.deltaTime);
if (direction.magnitude < AttackDistance)
{
anim.SetBool ("isWalking", false);
anim.SetBool ("isAttacking", true);
anim.SetBool ("isIdle", false);
}
}
}
else
{
anim.SetBool ("isWalking", false);
anim.SetBool ("isAttacking", false);
anim.SetBool ("isIdle", true);
}
}
}
↧