Hi guys, i come back for help So first i spawn zombies using a prefab, and spawn them over and over again. But when i kill one zombie, he play his death animation and his body remain on the ground (and i delete the character controller) that’s what i want. But what i don’t want is when one zombie die, the other suddenly stop moving, just play the walk animation over and over again, still have collision etc, but just don’t move. Here’s my code:
First the zombie script:
Zombie script
using UnityEngine;
using System.Collections;
public class Zombie : MonoBehaviour
{
public float speed;
public float range;
public CharacterController controller;
public Transform player;
public AnimationClip run;
public AnimationClip idle;
public AnimationClip attack1;
public AnimationClip death;
public static bool dead;
private bool isAttacking;
private int health;
// Use this for initialization
void Start()
{
health = 1000;
dead = false;
isAttacking = false;
animation[attack1.name].wrapMode = WrapMode.Once;
}
// Update is called once per frame
void Update()
{
if (animation.IsPlaying(attack1.name) == false)
{
isAttacking = false;
}
if (!dead && !isAttacking)
{
if (!inRange())
{
chase();
}
else if (!isAttacking)
{
attack();
}
}
Debug.Log(health);
}
public void getHit(int damage)
{
health = health - damage;
if (health <= 0 && !dead == true)
{
dead = true;
animation.Play(death.name);
}
else if (health <= 0)
{
Destroy(this.controller);
Destroy(this);
}
}
bool inRange()
{
return (Vector3.Distance(transform.position, player.position) < range);
}
void chase()
{
transform.LookAt(player.position);
controller.SimpleMove(transform.forward * speed);
animation.Play(run.name);
}
void attack()
{
isAttacking = true;
animation.Play(attack1.name);
player.GetComponent<Combat>().getHit(10);
}
}
I send bullet out of the gun of my character with this script (to deal damage)
Bullet Script
using UnityEngine;
using System.Collections;
public class collisionDetection : MonoBehaviour {
public GameObject target;
void OnCollisionEnter(Collision collide)
{
if (collide.gameObject)
{
Destroy(this.gameObject);// destroy the bullet
target = collide.gameObject;
target.GetComponent<Zombie>().getHit(10);//deal damage to zombie
}
}
}
And I add that when the zombie are paralyzed, and another one spawn, they suddenly move again, i just don’t understand x)
Thanks in advance for your help!