Hello,
I am creating a survival game. I have an animated enemy AI. When I duplicate it by pressing ctrl+d, the duplicated enemy does not behave as real one namely animations are not working. How can I duplicate an enemy so that new ones do the same job as real one?
Thank you for your help in advance!
Here is my enemy ai code:
public class EnemySkeleton : MonoBehaviour {
public Transform player;
static Animator anim;
public int currentHealth = 3;
PlayerScore playerScore;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
playerScore = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerScore>();
}
// Update is called once per frame
void Update () {
if (currentHealth <= 0)
{
return;
}
if (Vector3.Distance(player.position, this.transform.position) < 10)
{
Vector3 direction = player.position - this.transform.position;
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
Quaternion.LookRotation(direction), 0.2f);
anim.SetBool("IsIdle", false);
if (direction.magnitude > 2)
{
WalkTowardsPlayer();
}
else
{
Attack();
}
}
else
{
StayIdle();
}
}
void WalkTowardsPlayer()
{
this.transform.Translate(0, 0, 0.1f);
anim.SetBool("IsWalking", true);
anim.SetBool("IsAttacking", false);
}
void Attack()
{
anim.SetBool("IsWalking", false);
anim.SetBool("IsAttacking", true);
}
void StayIdle()
{
anim.SetBool("IsWalking", false);
anim.SetBool("IsAttacking", false);
anim.SetBool("IsIdle", true);
}
public void Damage(int damageAmount)
{
currentHealth -= damageAmount;
if (currentHealth <= 0)
{
playerScore.collectedScore += 10;
anim.SetBool("IsDeath", true);
}
}
}