- Home /
I made my code so that if i hit an enemy its health will go down but when i hit the enemy the health doesnt go down. please help
pragma strict
var Health = 100;
function ApplyDammage (TheDammage : int)
{
Health -= TheDammage;
}
Comment
#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))
{
Distance = hit.distance;
if (Distance < $$anonymous$$axDistance)
{
hit.transform.Send$$anonymous$$essage("ApplyDammage", TheDammage, Send$$anonymous$$essageOptions.DontRequireReceiver);
}
}
}
}
Answer by YoungDeveloper · Oct 11, 2013 at 10:06 AM
You can actually set the maxdistance as 3rd physics.raycast argument, instead of hit itself, so you wont need those two distance variables.
if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), 1.5))
And 1.5 is a very small number, you have to be very close for it to work, try setting it to 5 or something. If that didnt work add debug message after fire1 button press and after physics and see which and which doesnt.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
how to make a enemy health/die script. 1 Answer
i need some code suggestions 1 Answer
Why is my enemy stopping before it reaches the player? 1 Answer
Hurting by Gravity force HELP!!! ? 2 Answers