Hello peeps, Im kind of new to Unity3D, but a few years ago I was making an FPS for a college project and now I have returned to make an FPS game for Android.
Anyways, I was following a tutorial by Brackeys on Youtube and I wanted to make a separate button for the Melee combat. I created the enemy and then a button and applied the script to the button, the button works fine to play the combat animation, but wherever I move in the map, and looking no where near the enemy, it still manages to kill it?
Im very new to Java script, but getting the hang. Thanks :)
Heres the button script
pragma strict
var TheWeapon : Transform;
var TheDamage : int = 50;
var Distance : float;
var MaxDistance : float = 1.5;
function Update ()
{
if(Input.touchCount > 0 )
{
for(var i : int = 0; i < Input.touchCount; i++)
{
var touch : Touch = Input.GetTouch(i);
if(touch.phase == TouchPhase.Began &&
guiTexture.HitTest(touch.position))
{
//Attack animation
TheWeapon.animation.Play("Attack");
//Attack Function
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
{
Distance = hit.distance;
if (Distance < MaxDistance)
{
hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
}
}
}
if (TheWeapon.animation.isPlaying == false)
{
TheWeapon.animation.CrossFade("Idle");
}
}
}
}