I have attached a health script to the enemies in my game, but at the moment the player weapons simply destroy on contact, can anyone help with assigning damage values to different weapons? I dont know if im supposed to do this within the health script or the destroy script.
Here is the health script im using:
using UnityEngine;
using System.Collections;
public class EnemyHealth : MonoBehaviour
{
public int maxHealth = 100;
public int curHealth = 100;
public float healthBarLength;
// Use this for initialization
void Start()
{
healthBarLength = Screen.width / 2;
}
// Update is called once per frame
void Update()
{
AddjustCurrentHealth(0);
}
void OnGUI()
{
GUI.Box(new Rect(10, 40, healthBarLength, 20), curHealth + "/" + maxHealth);
}
public void AddjustCurrentHealth(int adj)
{
curHealth += adj;
if (curHealth < 0)
curHealth = 0;
if (curHealth > maxHealth)
curHealth = maxHealth;
if (maxHealth < 1)
maxHealth = 1;
healthBarLength = (Screen.width / 4) * (curHealth / (float)maxHealth);
}
}
Your current health script seems perfectly fine. I would have this type of code attached to any bullet or other damaging object you make:
// Assuming somewhere on the script you have:
float damage = -10f;
OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Enemy" )
{
EnemyHealth eHealth = other.GameObject.GetComponent<EnemyHealth>();
eHealth.AdjustCurrentHealth(damage);
}
Destroy(this.gameObject);
}
Another way you could go that might make things easier, is to implement health as an interface and just have IHealth stuck onto anything that can be hurt then your TriggerCode could look like this without checking for tags:
OnTriggerEnter(Collider other)
{
IHealth target = other.gameObject as IHealth;
if (target != null)
target.AdjustCurrentHealth(damage);
Destroy(this.gameObject);
}
I added this code to my destroy by contact script, but it is giving me a bunch of error messages:
Error CS1061 ‘Collider’ does not contain a definition for ‘GameObject’ and no extension method ‘GameObject’ accepting a first argument of type ‘Collider’ could be found (are you missing a using directive or an assembly reference?)
Error CS1061 ‘EnemyHealth’ does not contain a definition for ‘AdjustCurrentHealth’ and no extension method ‘AdjustCurrentHealth’ accepting a first argument of type ‘EnemyHealth’ could be found (are you missing a using directive or an assembly reference?)
Error Type EnemyHealth' does not contain a definition for AdjustCurrentHealth’ and no extension method AdjustCurrentHealth' of type EnemyHealth’ could be found (are you missing a using directive or an assembly reference?)
In your update function because it is sort of useless having a function that is called every frame, when that is what the Update function does already.
Now if you want a damage system you can use OnTriggetEnter; (This will be in the script on the Object that will be hitting the player, for example if you are using a gun, put the script in the bullet. Or if you are using a sword, in the sword since that will be the only object hitting the player)
for example:
There is no need for the AdjustHealth function as you can just access it straight from the script.
if it is a bullet you will also want to delete it on entry;
You will find that if you are using a sword, it will keep afflicting damage every frame that the sword is in the player.
You can fix this by putting a simple timer;
Thanks for the additional help BubyMB**,** however i’m still having problems. I just realised there is no destroy script attached to my bullets. The Destroy by Contact script is attached to the enemies themselves. I tried adding your code to a fresh script and attached it to a bullet but all it did was destroy my bullet almost as soon as it spawned.