- Home /
Good design when starting animation and triggering event in animation?
My goal is to end up with a very simple API for all my classes.
To make this question straightforward, let's say the class I'm working on is called Enemy.
The enemy game object I attach this script to will have a dash animation.
In this dash animation, the character shouldn't actually move left or right until half way through the animation.
I've attached an event to the halfway point of this animation, which calls on the Enemy's Dash() function.
Now here's my pickle. In order to initiate the dash animation, I was thinking I should be able to call on this Dash() function and call it a day. And then if the Dash() function is called from the animation, it will actually move the player.
Here's the basic idea for the code I have in mind:
public void Dash()
{
//IsDashing will be false if called from inside Script
if ( ! IsDashing )
{
IsDashing = true;
animator.SetBool( "IsDashing", true ); //start the animation
return;
}
//The code will reach this point when the function is called from the Animation event
velocity.x = 100;
IsDashing = false;
}
This would be fine with me, but I don't like that it's totally dependent on the fact that there must be an animation event triggering this function again. I don't think it makes sense to have to call a function twice to get the functionality you want.
However, I also don't like the idea of just setting the bool to true outside this Dash function, because then I'll have a Dash() function which I can't even explicitly call when implementing the AI. If I were to let a partner work with this script, they'd be wondering why on earth they couldn't call the Dash() function when programming the AI. It doesn't make sense for an API to have functions a programmer can't call lol.
I'm sure people have run into little situations like this so I'm just curious how you've handled it?
I'll be sure to upvote any answers or comments that aid me in solving this issue whatsoever.
EDIT: I realized that this problem could probably be solved by adding a public function SetVelocityX( float velocityX ), but I don't think Unity has a way of passing a variable as an argument in an animation event. It expects you to hardcode the float value which really isn't an option.
If you convert this comment to an answer I'll accept it. I just realized Animation Events have access to PRIVATE functions. This functionality combined with your idea of a DashCallBack function is perfect because a programmer only has to know about the Dash function, and the Dash function is not dependent on being called twice. Thanks a lot.
Answer by smoggach · Sep 05, 2014 at 03:12 AM
If the velocity depends on how far into the animation you are then it makes perfect sense to set the velocity from a callback from the animation.
You can use animator.GetBool instead of keeping track of IsDashing yourself. I would also recommend separating the functionality you have here into two functions. One will be your Dash function which is called by a script or something when you want to dash. It will change your animator state by setting the bool. The other is called DashCallback which is what you call from the animation event. There you can perform some logic to decide how to set the velocity if you even should. You'll also need a DashFinished function to set the animator state back to off when the animation finishes.
There's nothing wrong with using the animator to trigger physical state changes.
Answer by Baste · Sep 05, 2014 at 08:31 AM
Just use two methods. Use something like StartDash() to get the Dash going, and then use another method - say DoDashMovement() or something like that - for the animation trigger.
Whenever you've written a method that does two clearly distinct things, you should realize that you actually need two different methods.