Hello, I’ve been at this for hours. I’ve got a level with a bunch of zombies placed from a prefab. They run around and try and kill the player.
The player has a weapon that casts a ray when mouse is down, and if it hits a zombie, it is meant to access that zombies health script and change its life value.
The problem I’m having, is when the ray hits any zombie, all zombies take damage. I’m not sure where I’m going wrong. This is my weapon script:
#pragma strict
private var fireRate : float;
private var nextFire : float;
var hit : RaycastHit;
var weaponDamage : float;
//Weapon sound effects
var shotgunFire : AudioClip;
function Start()
{
//Set rate of fire
fireRate = 2;
weaponDamage = 1;
}
function Update()
{
if (Input.GetButton ("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
audio.PlayOneShot(shotgunFire);
//Cast ray, if hit zombie, deal damage to selected zombie
if (Physics.Raycast (transform.position, transform.forward, hit, 100))
{
//This doesn't draw a line, I mustn't be using it correctly
Debug.DrawLine (transform.position, transform.forward);
if (hit.collider.gameObject.tag == "zombie")
{
var enemyLifeScript = hit.collider.GetComponent(ZombieHealth);
if (enemyLifeScript)
enemyLifeScript.life -= weaponDamage;
//Other Things I've tried:
//hit.collider.gameObject.GetComponent(ZombieHealth).life = hit.collider.gameObject.GetComponent(ZombieHealth).life - weaponDamage;
//ZombieHealth.life = ZombieHealth.life - weaponDamage;
}
}
}
}
@script RequireComponent(AudioSource)