Hi, I am trying to write two scripts (RifleBullet and AnimalHealthbar). RiffleBullet accesses the AnimalHealthbar which is attached too all enemies in the game and applies the damage.
Problem being that if lets say I have 4 boxes (1,2,3,4) in a line… If I shoot at box 4 continuously, box 1 will die, then 2, then 3 and so on. It seems that the script is working as a single health pool and if my bullet hits anything with the tag enemy, the script will lose health and everytime it reaches 0 an enemy will die (Not the one I am targeting) and then it will move on to the next. How do I set it up to where each enemy has it’s own health… I’ve searched everywhere and I can’t find the answer to what I am doing wrong.
Here is my code, PS: My game is coded in C#
using UnityEngine;
using System.Collections;
/// <summary>
///
/// This is the 7.5mm bullet script.
///
/// This script is attached to the 7.5mmBullet.
///
/// </summary>
public class RifleBullet : MonoBehaviour {
//----------Variables Start-------------
//A quick reference.
private Transform myTransform;
//The projectiles flight speed.
private float projectileSpeed = 10;
//Prevent the projectile from causing
//further harm once it has hit something.
private bool expended = false;
//A ray projected in front of the projectile
//to see if it will hit a recognisable collider.
private RaycastHit hit;
//The range of that ray.
private float range = 1.5f;
//The life span of the projectile.
private float expireTime = 5;
//--------------Variables End------------------
// Use this for initialization
void Start ()
{
myTransform = transform;
//As soon as the projectile is created start a countdown
//to destroy it.
StartCoroutine(DestroyMyselfAfterSomeTime());
}
// Update is called once per frame
void Update ()
{
//Translate the projectile in the up direction (the pointed
//end of the projectile).
myTransform.Translate(Vector3.up * projectileSpeed * Time.deltaTime);
//If the ray hits something then execute this code.
if(Physics.Raycast(myTransform.position,myTransform.up, out hit, range)
expended == false)
{
//If the collider has the tag of Floor then..
if(hit.transform.tag == "Enemy")
{
//If hit and enemy, destroy self.
Destroy(myTransform.gameObject);
//Access the AnimalHealhBar Script to apply damage.
GameObject zombie = GameObject.Find("Zombie");
AnimalHealthbar aHealthScript = zombie.GetComponent<AnimalHealthbar>();
aHealthScript.wasHitByRifle = true;
}
}
}
IEnumerator DestroyMyselfAfterSomeTime()
{
//Wait for the timer to count up to the expireTime
//and then destroy the projectile.
yield return new WaitForSeconds(expireTime);
Destroy(myTransform.gameObject);
}
}
And the AnimalHealthBar Script
using UnityEngine;
using System.Collections;
public class AnimalHealthbar : MonoBehaviour {
//-------------Variables Start---------------
//The health bar texture is attached to this in the inspector.
public Texture healthTex;
//Quick references.
private Camera myCamera;
private Transform myTransform;
private Transform triggerTransform;
//These are used in determining whether the healthbar should be drawn
//and where on the screen.
private Vector3 worldPosition = new Vector3();
private Vector3 screenPosition = new Vector3();
private Vector3 cameraRelativePosition = new Vector3();
private float minimumZ = 1.5f;
//These variables are used in defining the health bar.
public int labelTop = 18;
public int labelWidth = 110;
public int labelHeight = 15;
public int barTop = 1;
public int healthBarHeight = 5;
public int healthBarLeft = 110;
public float healthBarLength;
public float adjustment = 1;
//Value of health and HealthBar
public float myHealth
public float maxHealth = 100;
public bool iWasHit = false;
private GUIStyle myStyle = new GUIStyle();
public bool wasHitByRifle = false;
public bool wasHitByAK = false;
//------------------Variables End------------------
void Start ()
{
myHealth = 100;
//Reference to our transform.
myTransform = transform;
//Reference to Camera's transform.
myCamera = Camera.main;
//FontSize
myStyle.fontSize = 12;
//FontSyle set to Bold.
myStyle.fontStyle = FontStyle.Bold;
//Allow the text to extend beyond the width of the label.
myStyle.clipping = TextClipping.Overflow;
}
// Update is called once per frame
void Update ()
{
//Figure out how long the health bar should be and to avoid a mathematical error set
//the health bar length to 1 if the the player's health falls below 1.
if(myHealth >= 1)
{
healthBarLength = (myHealth / maxHealth) * 100;
}
if(myHealth <= 0)
{
Destroy(myTransform.gameObject);
}
if(wasHitByRifle == true)
{
//Create the function to take away 55-90 health.
myHealth -= (Random.Range(55,100));
wasHitByRifle = false;
}
if(wasHitByAK == true)
{
HurtOnlyMe();
}
}
private void HurtOnlyMe()
{
//Create the function to take away 55-90 health.
myHealth -= (Random.Range(4,8));
wasHitByAK = false;
}
void OnGUI ()
{
//Only display the player's name if they are in front of the camera and also the
//player should be in front of the camera by at least minimumZ.
if(cameraRelativePosition.z > minimumZ)
{
//Set the world position to be just a bit above the player.
worldPosition = new Vector3(myTransform.position.x, myTransform.position.y + adjustment,
myTransform.position.z);
//Convert the world position to a point on the screen.
screenPosition = myCamera.WorldToScreenPoint(worldPosition);
//Draw the health bar and the grey bar behind it.
GUI.Box(new Rect(screenPosition.x - healthBarLeft / 2,
Screen.height - screenPosition.y - barTop,
100, healthBarHeight), "");
GUI.DrawTexture(new Rect(screenPosition.x - healthBarLeft / 2,
Screen.height - screenPosition.y - barTop,
healthBarLength, healthBarHeight), healthTex);
}
}
}