- Home /
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!
Could this be related? https://answers.unity.com/questions/171338/animationevent-triggered-multiple-times.html
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
I think you're right. $$anonymous$$aybe when an animation frame stays on for several game frames the event gets repeatedly called?
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.
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.
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?
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.
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.
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?
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
Follow this Question
Related Questions
Animator bool = true on TriggerEnter? 0 Answers
How to add WAKE_UP animation before the character enters IDLE state? 1 Answer
How to start animation after object is destroyed ? 0 Answers
Can you use triggers in script? 2 Answers
Melee Combat Collision 2 Answers