• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sendatsu_Yoshimitsu · May 28, 2014 at 09:55 AM · c#raycastprogrammingraycasthit

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sendatsu_Yoshimitsu · May 28, 2014 at 12:17 PM 0
Share

Ooh perfect, thank you! :)

avatar image Sendatsu_Yoshimitsu · May 28, 2014 at 12:44 PM 0
Share

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.

avatar image meat5000 ♦ · May 28, 2014 at 09:05 PM 0
Share

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?

avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

I'm trying to get a Raycast Laser weapon to tell whatever it hits to do damage, and its not working. im kindof a noob... 0 Answers

C# raycast shooting script broken 1 Answer

Having a RaycastHit event create a component? 1 Answer

Trouble with checking Raycast 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges