- Home /
Raycast executing hundreds of times
I'm using a raycast to check if any enemies are directly in front of my player when they attack. The player's Attack script broadcasts to an event handler script using CSharpMessenger, and the handler in turn checks for collision and activates the ApplyDamage function on each enemy:
void Attack(){
anim.SetTrigger("Punch");
Messenger<Vector3,Vector3,int>.Broadcast("melee attack made", transform.position, transform.forward, damage);
Debug.Log ("Broadcasting attack");
}
void Update () {
Messenger<Vector3,Vector3,int>.AddListener ("melee attack made", EvaluateAttack);
}
public void EvaluateAttack(Vector3 origin, Vector3 direction, int damage){
RaycastHit hit;
if (Physics.Raycast (origin, direction, out hit, 25.0f) && hit.transform.tag == "Enemy") {
SuccesfulAttack(hit, damage);
}
public void SuccesfulAttack(RaycastHit target, int damage){
target.collider.SendMessage("ApplyDamage", damage);
Debug.Log ("Executing attack");
}
public void ApplyDamage(int damage){
curVital = curVital - damage;
DisplayVital ();
if (curVital < 0)
curVital = 0;
}
Everything works, except when a ray hits an enemy, instead of executing once it keeps going- using debug.logs I found that SuccesfulAttack() is what's executing hundreds of times, but I can't see why.
Answer by meat5000 · May 28, 2014 at 10:18 AM
It stems from your Update
void Update ()
{
Messenger<Vector3,Vector3,int>.AddListener ("melee attack made", EvaluateAttack);
}
It keeps evaluating with no condition. When the raycast is true it still keeps evaluating and as the conditions are met it keeps executing subsequent functions, every frame.
Put a Debug.Log in Evaluate function. This will show you what I mean.
Hmm, I understand the problem, but I'm struggling to figure out what condition will keep the listener from spam$$anonymous$$g EvaluateAttack while still letting it fire enough to handle multiple things attacking within a few frames of each other.
Perhaps a check to deter$$anonymous$$e if the hit target is different from the last? If it's the same, don't re-fire the function.
A time delay?
Do you use collision function at all?
Answer by Sendatsu_Yoshimitsu · May 29, 2014 at 05:58 AM
Hmm, I think I must be missing something- I tried setting up a bool to indicate whether or not I wanted the messenger to fire, and toggled it with each attack (which in turn is constrained by a cooldown timer) as follows:
void Update () {
if (attackTimer > 0)
attackTimer -= Time.deltaTime;
else
if (attackTimer < 0) {
attackTimer = 0;
canAttack = true;
}
if (Input.GetKeyDown(KeyCode.Alpha1)){
if (attackTimer==0){
attackTimer = coolDown;
Attack();
}
//gameObject.SendMessage("EvaluateAttack", transform.position);
}
}
anim.SetTrigger ("Punch");
Messenger<bool>.Broadcast ("Toggle melee listener", canAttack);
canAttack = false;
Messenger<Vector3,Vector3,int>.Broadcast ("melee attack made", transform.position, transform.forward, damage);
public void ToggleListener(bool toggler){
canAttack = toggler;
}
}
RaycastHit hit;
if (Physics.Raycast (origin, direction, out hit, 25.0f) && hit.transform.tag == "Enemy" && canAttack == true) {
Debug.Log("Attack registered");
SuccesfulAttack(hit, damage);
}
And it still spams SuccessfulAttack. It seems like I'm massively over-complicating what should be a simple operation: is there any alternative to placing my event listeners in Update?