[Android] Why does my enemy die no matter where I am in the 3D environment?

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).

–

Thanks 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 :/

–

Is this necessary? transform.TransformDirection Also, I don't use sendmessage so I'm not familiar, but isn't it a broadcast?

–

Well 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

–

Is distance staying the same with TransformDirection in or out? Try this if (Physics.Raycast (transform.position, Vector3.forward, hit, MaxDistance))

–

1 Answer

1

Ah wait, I’ve done it guys :P, turns out I needed a GUI script with Rect LOL, got the new updated one here, basically it shows my texture where my button is on the script and when I hit it, the ray shoots where im looking, took a while but I got there.

Appreciate the help, thanks!

var ButtonTexture : Texture ;
var TheWeapon : Transform;
var TheDamage = 100;


function Update()
{
	var hit : RaycastHit;
	var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
	
	//Touches + Co-ordinates Of button placement via (Rect)
	for(var i: int = 0;i < Input.touchCount; i++)
	{
    	var touch : Touch = Input.GetTouch(i);
    	if(Rect(0, 135, 100, 55).Contains(touch.position) && touch.phase == TouchPhase.Began)
    		{
      			 print("Does it work yet?");
  				//My Animation
       			TheWeapon.animation.Play("Attack");
       			if (Physics.Raycast (ray, hit, 100))
					{
						hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
					}
    		}
	}	
	if (TheWeapon.animation.isPlaying == false)
					{
						TheWeapon.animation.CrossFade("Idle");
					}
}
 //GUI texture with Co-ordinates
function OnGUI(){GUI.DrawTexture(Rect(0,135,100,55),ButtonTexture );}