Misunderstanding of RaycastHit?

I’ve been following a tutorial online for a while and so far its gone pretty well. The only problem I have is with RaycastHit. Now at first glance everything seems to be working pretty well, but every now and then it seems as if I get two hits. Basically I have it set up so that every time I press the mouse button, an animation will go off with the player swinging a club and a ray will be sent from the player(actually a gameObject parented by the first person camera) to the enemy. But sometimes instead of the gameObject being destroyed in two hits, every now and then, its destroyed in only one. After asking someone on another forum about it, they said this

Also here is some Info from the Debug.Log

13.24059
UnityEngine.Debug:Log(Object)
Melee:AttackDamage() (at Assets/Scripts/Melee.js:25)

0.9856399
UnityEngine.Debug:Log(Object)
Melee:AttackDamage() (at Assets/Scripts/Melee.js:25)

gameObject destroyed in 1 hit even with the first hit way over 1.5

6.858145
UnityEngine.Debug:Log(Object)
Melee:AttackDamage() (at Assets/Scripts/Melee.js:25)

0.6300163
UnityEngine.Debug:Log(Object)
Melee:AttackDamage() (at Assets/Scripts/Melee.js:25)

0.6300163
UnityEngine.Debug:Log(Object)
Melee:AttackDamage() (at Assets/Scripts/Melee.js:25)

gameObject destroyed in 2 hits acting as it should normally

So is there something I don’t understand about RaycastHit? Why would it " apply damage to the enemy based on whether or not the last hit was successful." Is it really doing this? Am I missing something?

#pragma strict

var TheDamage : int = 50;
var Distance : float;			
var MaxDistance : float = 1.5;
var TheSystem : Transform;
    

function Update ()								 
{
	if(Input.GetButtonDown("Fire1")) 
	{
		
		animation.Play("Attack"); 
	}

}

function AttackDamage () 
{
	var hit : RaycastHit;
	if(Physics.Raycast (TheSystem.transform.position, TheSystem.transform.TransformDirection(Vector3.forward),hit))
	{											 
		Distance = hit.distance;
		Debug.Log(Distance);	
	{ 

	if (Distance < MaxDistance)
        {						
		EnemyLogic.ApplyDamage(TheDamage);
	}

}
#pragma strict

static var Health = 100;


function Update ()
{
	if (Health <= 0)
	{
		Dead ();
	}
}

static function ApplyDamage (TheDamage: int)
{
	Health -= TheDamage;
}

function Dead ()
{
	Destroy (gameObject);
}

I think the wider question is why are you using raycasts for melee hits?

It may be better to use colliders, especially if you have weapons and check their intersections with the enemy.

This was just from a tutorial. My point was that it doesn’t add up.

Picture this:

You swing and detect a successful attack from a distance of 15.
You swing again but the raycast misses. However distance is still equal to 15 which makes a successful attack.

This doesn’t attribute to the double attack. But I think that could be a double mouse click or something like that. Try adding a delay between how often the player is allowed to attack.

Thats a good point, I kind of thought about that, but wasn’t sure how to fix it. I’m mostly just picking at the code though I admit it. I still love unity, which is why I want to catch something while i’m still learning. So I don’t know if I did something weird, or if its just being buggy, but anyways I mostly just do it to try and gain a better understanding.

This is untested but should at least give you the right idea:

var TheDamage : int = 50;
var MaxDistance : float = 1.5;
var TheSystem : Transform;

function Update ()
{
    if(!animation.IsPlaying("Attack")  Input.GetButtonDown("Fire1"))
    {
        animation.Play("Attack");
    }
}

function AttackDamage ()
{
    var hit : RaycastHit;
    if(Physics.Raycast (TheSystem.transform.position, TheSystem.transform.TransformDirection(Vector3.forward),hit))
    {
        if (hit.distance < MaxDistance)
        {                      
            EnemyLogic.ApplyDamage(TheDamage);
        } 
    }
}

Awesome! thank you, I’ll give it a try