My health script is not working and i can't find the solution. The problem is that if I hit the enemy (a cube for example) with the bullet he won't die/disappear.
#This is my health script
#pragma strict
var Health : int = 100;
var bullet : GameObject;
var damage : int = 25;
function Update ()
{
if(Health < 0)
{
DIE();
}
}
function OnTriggerEnter (hit : Collider)
{
if(hit.gameObject.tag == "Bullet")
{
bullet = hit.gameObject;
TakeDamage ();
}
}
function TakeDamage ()
{
var GunPreferences : Bullet = bullet.GetComponent("Bullet");
damage = GunPreferences.Damage;
Health -= damage;
}
function DIE ()
{
Debug.Log("I'm dead");
Destroy(gameObject);
}
#My Gun Script
var Bullet : Rigidbody;
var Spawn : Transform;
var BulletSpeed : float = 1000;
var ReloadTime : float = 2;
var AmmoInMag: float = 30;
var IsFullAuto = true;
static var AmmoLeft : float;
static var ReloadTTime : float;
private var CanFire = true;
var FireRate = 0.1;
function Start () {
AmmoLeft = AmmoInMag;
ReloadTTime = ReloadTime;
}
function Update () {
if(IsFullAuto == false){
if(Input.GetButtonDown("Fire1")){
if(AmmoLeft > 0){
BroadcastMessage("FireAnim");
Fire();
}
}
}
else{
if(Input.GetButton("Fire1")){
if(AmmoLeft > 0){
BroadcastMessage("FireAnim");
Fire();
}
}
}
if(AmmoLeft == 0)
{
Reload();
}
if(AmmoLeft < 0){
AmmoLeft = 0;
}
}
function Fire(){
if(CanFire == true){
var bullet1 : Rigidbody = Instantiate(Bullet,Spawn.position,Spawn.rotation);
bullet1.AddForce(bullet1.transform.forward *BulletSpeed);
CanFire = false;
yield WaitForSeconds(FireRate);
CanFire = true;
AmmoLeft -= 1;
audio.Play();
}
}
function Reload(){
CanFire = false;
BroadcastMessage("ReloadAnim");
yield WaitForSeconds(ReloadTime);
CanFire = true;
}
Thanks in advance!!
↧