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.