Hey all,
I’ve got a script which shoots a RayCast at an enemy, this is then meant to reduce a set amount of damage from the health script associated with that enemy. However currently, it is reducing the health of all enemies and I can’t see why (from what I can tell I am telling it to damage only the instance of the object).
Weapon Shooting script (You’ll want to look at the “private void fire()” section:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(AudioSource))]
public class WeaponShooting : MonoBehaviour
{
private Animator animate;
public int bulletsPerMag = 30;
public int maxBullets = 300;
public int bulletsLeft = 200;
public int currentBullets;
public int shootDamage = 5;
public float range = 50f;
public float fireRate = 0.1f;
public Transform shootPoint;
public GameObject flash;
float fireTimer;
public AudioClip akshoot;
public AudioClip akreload;
public AudioSource AudioSource;
public bool reloadisrunning;
public Text ammoRemaining;
public EnemyHealth enemyHealthScript;
// Use this for initialization
void Start()
{
animate = GetComponent<Animator>();
currentBullets = bulletsPerMag;
AudioSource = GetComponent<AudioSource>();
SetInitialReferences();
}
void SetInitialReferences() {
ammoRemaining = GameObject.Find("Ammo Remaining").GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.Mouse0))
{
Fire(); // Executes the fire function if the mouse button is pressed or held.
}
if (fireTimer < fireRate)
fireTimer += Time.deltaTime;
if (currentBullets <= 0 || Input.GetKey(KeyCode.R) && reloadisrunning == false)
{
if (bulletsLeft >= 1 && currentBullets != 30)
{
PlayReloadSound();
}
StartCoroutine(reloadWait());
}
ammoCountUI();
}
void ammoCountUI()
{
string bulletsLeftS = bulletsLeft.ToString();
string currentBulletsS = currentBullets.ToString();
ammoRemaining.text = currentBulletsS + "/" + bulletsLeftS;
}
void FixedUpdate()
{
AnimatorStateInfo info = animate.GetCurrentAnimatorStateInfo(0);
if (info.IsName("Fire")) animate.SetBool("Fire", false);
}
/// ----------------------------------- Firing the Gun -------------------------------------------------
private void Fire()
{
if (fireTimer < fireRate) return; // This returns to the update
[B] if (currentBullets >= 1 && reloadisrunning == false)
{
StartCoroutine(flashOn());
RaycastHit hit;
if (Physics.Raycast(shootPoint.position, shootPoint.transform.forward, out hit, range))
{
if (hit.rigidbody.gameObject.layer == LayerMask.NameToLayer("Enemy"))
{
enemyHealthScript = hit.collider.gameObject.GetComponent<EnemyHealth>();
if (enemyHealthScript != null)
{
enemyHealthScript.dtak47();
}[/B]
}
}
currentBullets--;
animate.CrossFadeInFixedTime("Fire", 0.01f); // Play the fire animation
animate.SetBool("Fire", true);
}
fireTimer = 0.0f; // This resets the fire timer
}
private void PlayShootSound()
{
AudioSource.clip = akshoot;
AudioSource.Play();
}
IEnumerator flashOn() //Muzzle Flash
{
flash.SetActive(true);
Debug.Log("flashOff");
PlayShootSound();
yield return new WaitForSeconds(0.15f);
Debug.Log("Timeup");
flash.SetActive(false);
if (Input.GetButton("Fire1"))
{
Fire();
}
}
/// ----------------------------------- Reloading the Gun -------------------------------------------------
private void PlayReloadSound()
{
AudioSource.clip = akreload;
AudioSource.Play();
}
IEnumerator reloadWait() //wait timer so that gun is not reloaded before the sound effectends
{
Debug.Log("wait start");
reloadisrunning = true;
yield return new WaitForSeconds(2f);
reloadisrunning = false;
Debug.Log("wait end");
Reload();
}
private void Reload()
{
if (bulletsLeft >= bulletsPerMag)
{
if (currentBullets == 0)
{
bulletsLeft = (bulletsLeft -= bulletsPerMag);
currentBullets = bulletsPerMag;
}
else if (currentBullets != 0)
{
bulletsLeft = (bulletsLeft - (bulletsPerMag - currentBullets));
currentBullets = bulletsPerMag;
}
}
else if (bulletsLeft <= bulletsPerMag)
{
if (currentBullets == 0)
{
currentBullets = bulletsLeft;
bulletsLeft = 0;
}
if (currentBullets != 0)
{
int dif = currentBullets + bulletsLeft;
bulletsLeft = (bulletsLeft - (bulletsPerMag - currentBullets));
currentBullets = dif;
}
if (bulletsLeft <= 0)
{
bulletsLeft = 0;
}
if (currentBullets >= bulletsPerMag)
{
currentBullets = bulletsPerMag;
}
if (currentBullets <= 0)
{
currentBullets = 0;
}
}
}
}
Health Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyHealth : MonoBehaviour {
public float health;
public int ak47damage = 10;
public EnemyHealth Instance;
// Use this for initialization
void Start()
{
SetInitialReferences();
health = 100;
}
// Update is called once per frame
void Update()
{
if (health < 1)
{
health = 0;
}
if (health > 99)
{
health = 100;
}
}
void SetInitialReferences()
{
Instance = this;
}
public void dtak47()
{
this.health -= ak47damage;
}
}
Any help would be appreciated.