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");
}
}
}
}
I don't see anything here. Just to make sure this code is causing the problem, I assume this line is being executed?: TheWeapon.animation.Play("Attack"); Also note that the values for TheDamage and MaxDistance will be the ones you see in the Inspector, not the ones in this code (assuming you've made in changes to these values).
– robertbuThanks for replying so fast! :), Aye the animation line is being executed with no problems, and yeah you can see The Damage and MaxDistance in the inspector, its just confusing why im not even looking at the enemy and it still manages to die :/
– duckduckmooseIs this necessary? transform.TransformDirection Also, I don't use sendmessage so I'm not familiar, but isn't it a broadcast?
– meat5000Well I took out transform.TransformDirection and it still does the same thing, I also just noticed that the distance is not changing value when I move, it stays at 5.85 even though the max distance is a higher value
– duckduckmooseIs distance staying the same with TransformDirection in or out? Try this if (Physics.Raycast (transform.position, Vector3.forward, hit, MaxDistance))
– meat5000