I am rather new to the coding side of Unity and I have a problem with my JavaSvcript code. I have two codes, both are supposed to work together to make the player attack an enemy and make it lose health, and die. These are my two codes:
var TheDamage : 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("ApplyTheDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
}
}
}
}
And the second is:
var Health = 100;
function Update ()
{
if(Health <= 0)
{
Dead();
}
}
function ApplyDamage (TheDamage : int)
{
Health = Health - TheDamage;
}
function Dead()
{
Destroy (gameObject);
}
Thank you for reading this and trying to help me.