• 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 UnholyCathar · Nov 19, 2018 at 10:09 PM · triggerboolanimation events

Shoot single fireball through animation event

Hello all! I'm having an issue in my game where I'm trying to have my animation change a bool in my script to trigger my boss character to shoot a a single fireball at a particular frame. I have an animation event that modifies a bool in my boss script that then fires a fireball.

The issue I'm having is that when the animation triggers the change of the bool, it actually shoots 4 fireballs at the same time. I think this is due to the Update method being called multiple times while that one frame that updates the bool is called. Even when I put a 'shootFireball = false' in the block that gets triggered in the script. The animation event for the frame seems to override this change.

My script looks something like:

 //Bool changed by animation event
 bool fireball = false;
 
 //Bool changed in script
 bool already_shooting = false;
 
 Update()
 {
 if(fireball)
 {
 //Change bool to not enter this state again
 fireball = false;
 
 //Void Function to shoot fireball
 ShootOneFireball();
 }
 }


The only work-around I've found is making the Void ShootOneFireball an IEnumarator with a delay to change a second 'Already_shooting' bool from true to false, but I feel like I should just be able to do this through the animation. Any ideas for fixing this?

And let me know if more info is needed. Thanks!

Comment
Add comment · Show 3
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 ecv80 · Nov 19, 2018 at 11:55 PM 0
Share

Could this be related? https://answers.unity.com/questions/171338/animationevent-triggered-multiple-times.html

avatar image UnholyCathar ecv80 · Nov 20, 2018 at 12:13 AM 1
Share

Although the issue is similar, I don't think the cause of their issue is the same as $$anonymous$$e. I only have 1 instance of the boss in the Scene with only 1 script being access by the anim-clip / animation event.

I think my issue is related to the animation event updating multiple times, or something like when that particular animation frame plays with the event that sets the bool, the animator says "since I am still in this frame, keep setting to True." so when is set to false, since the script is running so much faster than the animation, the animation keeps telling the script on each update "while in this frame, = True"

I'll post my workaround in a few $$anonymous$$utes. Thanks for replying though! :D

avatar image ecv80 UnholyCathar · Nov 20, 2018 at 11:31 AM 0
Share

I think you're right. $$anonymous$$aybe when an animation frame stays on for several game frames the event gets repeatedly called?

2 Replies

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

Answer by s_awali · Nov 20, 2018 at 01:16 PM

Instead of modifying a bool and checking its value each frame, you should use the animation event to trigger your ShootFireball function.

Comment
Add comment · Show 7 · 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 UnholyCathar · Nov 20, 2018 at 08:41 PM 0
Share

I don't think Unity lets you call functions through the animation event, at least not through the GUI. Unless there's some prefix I should use when setting up the ShootFireball() function? Currently in my script I have it as a public function, but the Animation window doesn't let me add it as a property.

avatar image ecv80 UnholyCathar · Nov 20, 2018 at 09:30 PM 0
Share

Yes you can, although I don't quite understand how exactly even after watching this one: link text But the thing is, if the event is indeed being evaluated 4 times, it wouldn't make a difference. No?

avatar image UnholyCathar ecv80 · Nov 20, 2018 at 11:05 PM 0
Share

I don't seem to have the same options in my Editor. $$anonymous$$aybe because I'd doing 2D animations ins$$anonymous$$d of 3D? I only seem to be able to setup events in the animation timeline on specific frames. Animation Events

Show more comments
avatar image
0

Answer by UnholyCathar · Nov 20, 2018 at 12:21 AM

My current work-around is to have a second variable called 'am_shooting' that gets updated in an IEnumerator with a short delay. So full code looks something like this:

 //Changed by animation event
 shoot_fireball = false;
 
 //Changed after check and after delay
 am_shooting = false;
 
 Update()
 {
 if(shoot_fireball && !am_shooting)
 {
 shoot_fireball = false;
 am_shooting = true;
 ShootFireball();
 StartCoroutine(FireballDelay());
 }
 }
 ShootFireball() {/*Shoot Fireball*/}
 
 IEnumerator FireballDelay() 
 {
 yield return new WaitForSeconds(0.1f);
 am_shooting = false;
 }

This works as intended and only allows 1 fireball to be shot. A bit roundabout but this is currently the best I've got at the moment. My only concern is potential problems if I decide to up the animation speed.

Comment
Add comment · Show 2 · 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 ecv80 · Nov 20, 2018 at 11:40 AM 1
Share

Another workaround could be to set an event at the start of your animation setting a counter of shots to 0, then in Update along with the current flag check if (shots<1) { shots++; ShootFireball(); } $$anonymous$$aybe?

avatar image UnholyCathar ecv80 · Nov 20, 2018 at 10:56 PM 0
Share

I think I'd still need to setup a timer for this to have the 'shots' counter return to 0 at some point so that I can shoot another fireball. I think the animation events may just not be perfectly ideal for setting up fine tuned triggers like this. (Unless I'm missing something, which is also entirely possible lol)

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

114 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can you use triggers in script? 2 Answers

Melee Combat Collision 2 Answers

How do I make my sprite go left or right when it hits a trigger? 0 Answers

How can I setup my bool/trigger in animator? 1 Answer

Object will not return to start position after player respawns 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