Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 tbugrara · Mar 04, 2012 at 02:03 AM · damagesendmessageinvoke

How can I delay dealing damage?

When clicking LMB, the Attack function below is called. However, in its current implementation, it sends the ApplyDamage message instantly and I would like it to wait half a second so that the damage can be applied in sync with the animation. Code below:

 void Attack()
 {
     if (Time.time > nextAttack)
     {
         Collider[] colliders = Physics.OverlapSphere(transform.position, AttackRange);
         foreach (Collider hit in colliders)
         {
             if (hit.tag == "Enemy" && Vector3.Distance(hit.transform.position, transform.position) <= AttackRange)
             {
                 hit.SendMessage("ApplyDamage", AttackDamage);
             }
         }
         nextAttack = Time.time + AttackRate;
     }
 }

I would have liked to use Invoke but I cannot pass the hit Collider through Invoke. I also thought about making a global variable but by the time the Invoked function could execute, the global variable might have changed as the for loop searched for enemies. It looks like my only option is to make a global array of Colliders, add a Collider to the array when it detects and enemy and after searching, send the ApplyDamage message to all Colliders in the list. Is there a better way to do this?

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

1 Reply

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

Answer by ByteSheep · Mar 04, 2012 at 06:56 AM

If you are already triggering the animation then you can go to your animation window and add an event that calls the function as soon as the animation has gotten to a certain point. To do all this just go to window > Animation and select your animation.. then double click or right click just under the timeline to add an event which will call a function, you can then specify which function should be called. This should work well for your purpose I think :)

Comment
Add comment · Show 1 · 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 tbugrara · Mar 04, 2012 at 07:01 AM 0
Share

Ahh I didn't know you could do that in the Animation window. I'll read up on it, thanks :D

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Damage sending with grenades. 1 Answer

SendMessage is working but the variable is not being updated on the other end 1 Answer

SendMessage vs MethodInfo.Invoke 1 Answer

Repeating Damage sendMessage 1 Answer

Send Message Not sending 2 Answers


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