- Home /
Enemy health not decreasing
Okay so I have a melee system and a enemy health system, they both work and let me play in game, however the enemy's health just does not decrease for some reason?
Here are both scripts
ENEMY HEALTH
pragma strict
ar Health = 100;
function ApplyDammage (TheDammage : int) { Health -= TheDammage; }
MELEE SYSTEM
#pragma strict
var TheDammage : int = 50;
var Distance : float;
var MaxDistance : float = 1.5;
function Update()
{
if (Input.GetButtonDown ("Fire1"))
{
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), hit))
{
Distance = hit.distance;
if (Distance < MaxDistance)
{
hit.transform.SendMessage("ApplyDamage", TheDammage, SendMessageOptions.DontRequireReceiver);
}
}
}
}
Try this :
#pragma strict
var TheDammage : int = 50;
var Distance : float;
var $$anonymous$$axDistance : float = 1.5;
function Update() {
if (Input.GetButtonDown ("Fire1")) {
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), hit)) {
Debug.Log("Raycast is working");
Distance = hit.distance;
if (Distance < $$anonymous$$axDistance) {
hit.collider.gameObject.Send$$anonymous$$essage("ApplyDamage", TheDammage, Send$$anonymous$$essageOptions.DontRequireReceiver);
}
}
}
}
You can get rid of the Debug.Log line if it works, I've put it in as a check to make sure that the raycast is hitting something. You may get an error if it does not hit what you expect it to hit, in which case you need to do a check for the correct object (an if statement) around your send message code(Which I thought may be wrong in your code above and have changed).
The new script Works the same as the old one which I have now binned, however enemy's health for some reason still does not decrease?
Did the Debug Window show the message "Raycast is working" when you ran the script?
I have changed script, and deleted debug.log line, however still for some reason there is no decrease in health? Ray-cast is working alright, I got a big list of ray-cast is working, I entered the game yet enemy's health is still not effected, the melee system works, gives me distance reading, I just can't get my head around it? I started writing up extra's on the enemy health script so the enemy disappears (death)
pragma strict
var Health = 100;
function update () { if(Health <= 0) Dead(); }
function ApplyDammage (TheDammage : int) { Health -= TheDammage; }
function Dead() { Destroy (gameObject); }
Yes! that done it, thank you so much, health reaches zero now however doesn't $$anonymous$$us, I see why this doesn't occur because your not telling the enemy to take any past the 0 point, this I prefer, perfect.
Answer by MrSoad · Nov 13, 2014 at 12:47 AM
Ok, so the problem is either the distance check(set the MaxDistance to 5 for a last test) which seems unlikly, or it is the send message. Change the send massage line to this :
hit.collider.gameObject.SendMessage("ApplyDamage");
And change this script to this :
#pragma strict
var Health : int = 100;
function ApplyDamage() {
Health = Health - 50;
if (Health <= 0) {
Debug.Log("I have sustained a lethal injury...");
Health = 0;
}
}
Making sure this script is on the collider object of the target, if the collider is on a child and this script is on the parent then you will have to script up through the objects hierarchy to find the the object that has this script attached(in your other script).
lol, I've just spotted the probable cause and corrected it, Damage was written as Dammage in your ApplyDamage function, fixed this typo, now give it a go :)