RaycastHit Randomly Gets 2 Hits

2019-08-24 19:00发布

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.

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

标签: unity3d
0条回答
登录 后发表回答