- Home /
Why Zombie clones dont take Player's HP?
Hi. I make Zombie.prefab with this code:
using UnityEngine;
using System.Collections;
public class FollowBotAI : MonoBehaviour {
Transform Leader;
float AISpeed = 1;
float MaxDistance = 10;
float MinDistance = 1.5f;
public bool attack = false;
public bool dead = false;
public int MaxHP = 5;
public int CurHP;
private Animator anim;
void Start () {
Leader = GameObject.FindGameObjectWithTag ("Player").transform;
anim = gameObject.GetComponent<Animator> ();
CurHP = MaxHP;
}
void Update ()
{
anim.SetFloat ("speed", AISpeed);
anim.SetBool ("attack", attack);
anim.SetBool ("dead", dead);
transform.LookAt (Leader);
AI ();
if (CurHP == 0)
{
dead = true;
}
}
void AI()
{
if (Vector3.Distance (transform.position, Leader.position) >= MinDistance) {
transform.position += transform.forward * AISpeed * Time.deltaTime;
attack = false;
} else
{
attack = true;
}
}
void AIDie()
{
Destroy (gameObject);
}
}
Main priority is true or false "attack". I have Player with this HP behavior code:
using UnityEngine;
using System.Collections;
public class HPbehavior : MonoBehaviour {
public GameObject zombie;
public int maxHP = 100;
public int curHP;
private FollowBotAI botAI;
void Start()
{
curHP = maxHP;
botAI = zombie.GetComponent<FollowBotAI> ();
}
void Update()
{
if (botAI.attack == true)
{
curHP = curHP - 1;
}
if (curHP <= 0)
{
Destroy(this.gameObject);
}
}
void OnGUI()
{
string textHP = "Health: " + (curHP);
GUI.Box (new Rect (Screen.width - 150, 20, 130, 20), textHP);
}
}
Well...if attack is true, HP is losing. I put my Zombi.prefab in to HPbehavior:
In my Zombi.prefab is Code FollowBotAI:
I dont know why Zombi(clones) dont take Player's HP. What is wrong with my Codes? I check if attack is real true, when zombie attacking Player. Yes, it is. But it ignore my condition in HPbehavior code. PLease help me...
untitled2.jpg
(290.9 kB)
Comment
Your answer
Follow this Question
Related Questions
Zombie Alien Mellee Attack 1 Answer
Ai Zombie Melee Attack script. 5 Answers
Zombie attack script help 1 Answer
How do I make it so that I can attack more than 1 enemy? 1 Answer
Follow Player Via Waypoints 1 Answer