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);
}