- Home /
Question by
evildumdum · Sep 01, 2014 at 10:15 AM ·
javascriptdestroy
Melle script, enemy destroyed without mouse command once in range.
so everything was working till i realized once i got in range of an enemy it got destroyed, i think this has something to do with the fact that i may not have it set up so mouse 1 apply's damage and instead what happens is it sends out a message apply damage once a frame once i get within 1.5 units of the enemy.
here is the Melle script:
#pragma strict
var Damage : 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", Damage, SendMessageOptions.DontRequireReceiver)
);
}
and here is the script attached the enemy:
#pragma strict
var Health = 100;
function Update ()
{
if (Health <= 0)
{
Dead();
}
}
function ApplyDamage (Damage : int)
{
Health -= Damage;
}
function Dead()
{
Destroy (gameObject);
}
As well as my 'Fire 1' settings

can someone help please?
screenshot 2014-08-31 16.59.52.png
(408.1 kB)
Comment
Answer by froYo · Sep 01, 2014 at 11:03 AM
Try this:
var Damage : 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", Damage, SendMessageOptions.DontRequireReceiver);
}
}
}
}
If my answer helped you sort out your problem, it would be great if you could mark it as accepted. Thanks :)
Your answer