how to spawn clones with animations
I am making a call of duty zombies styled game. I have a skeleton enemy which has three animations, idle for if the player is over 1000 meters away, walk and attack for if the skeleton is within 5 metres of the player. I have tried to make spawners for the skeletons, but the spawned skeletons are stuck with the animation that the main skeleton was doing when they were spawned. Therefore, the skeletons will never attack the player. Please help! here is my code for the main skeleton:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Chase : MonoBehaviour {
public Transform player;
static Animator anim;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update ()
{
if(Vector3.Distance(player.position, this.transform.position) <10000)
{
Vector3 direction = player.position - this.transform.position;
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
Quaternion.LookRotation(direction), 1f);
anim.SetBool("isIdle", false);
if(direction.magnitude > 3)
{
this.transform.Translate(0,0,0.05f);
anim.SetBool("isWalking", true);
anim.SetBool("isAttacking", false);
}
else
{
anim.SetBool("isAttacking", true);
anim.SetBool("isWalking", false);
}
}
else
{
anim.SetBool("isIdle", true);
anim.SetBool("isWalking", false);
anim.SetBool("IsAttacking", false);
}
}
}
Here is my code for the spawners:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Timedspawn : MonoBehaviour {
public GameObject spawner;
public bool stopSpawning = false;
public float spawnTime;
public float spawnDelay;
// Use this for initialization
void Start()
{
InvokeRepeating("SpawnObject", spawnTime, spawnDelay);
}
public void SpawnObject()
{
Instantiate(spawner, transform.position, transform.rotation);
if (stopSpawning)
{
CancelInvoke("SpawnObject");
}
}
}