(this recipe is part of the footvolley game menu )
Sending global Events
We’ll now use a global event. Global events are different from normal events in the way that these events, once fired, are visible by all objects that got a FSM attached. Instead of detecting collision each time our collider is visible, We’ll sent a Kick event that’ll be visible by our soccer ball object and that’ll tell to our ball that it’s getting kicked ONLY when we release our shot button.
In our first version we’ll just add an impulse force to the ball, that make it go away.
You can think that sending an event to the ball is adding an unnecessary layer of complexity because just adding force to the soccer ball directly from the player FSM will do the trick, and you’ll be right. But in later gameplay we’ll add some function to the ball that will handle its trajectories itself without any physics, so we just anticipate this step a little bit.
00:04 Creating global Events
In the FSM windows, go to Events tab, then check the tiny box at the left of our “KickReleased” event, it set it up as a global event, that will be sent through other FSMs.
Like this when we release our shoot button, we sent a KickReleased Event to all FSMs.
00:12 Freezing gameplay
Now we modify our logic a little, instead of getting back to idle as soon as we release space button, we’ll freeze our player a bit. Like this we stop the user to hit continuously space with the hope to hit the ball on the fly : if he release space, he has to be sure because he’ll be freeze 0.5 sec after that.
So we create a new state : ReleaseKick, right click/Add state/ “releaseKick”.
And on our “isKicking” state, change our KickRelease transition link and point it to our ReleaseKick state.
On RelaseKick state, add a Wait action, set it to 0.1 second, and choose Finished as Finish event.
Add a transition Event “Finished” and link it to our idle state. And voila, the loop is closed.
01:31 Checking Collision after kickRelease
We now change another thing : the collision check. Until now we were testing collision when our collider was visible, but in game we want test the collision only after we release our kick button (meaning we’ve charged our shot and decided that we kick it back). So we’ll just add a boolean to check : isKickReleased.
We create a global boolean isKickReleased on our variables FSM window tab.
We go in our ReleaseKick state and set it our isKickReleased bool to true when releasing our shot with the Set Bool Value action.
Then once we released our shot we go back to our Idle state and set the isKickReleased as False, as we’re kicking nothing.
02:59 Changing Collider logic
Until now we were directly checking if the ball was inside the collider zone to add a force; now we’ll wait that the player release our kick button before testing the collision.
Go to the t_collider FSM, add a new state : isKicking. In this state we will include a Bool Test action . We will test if our Kickrelease global boolean is true or false, based on the result we will sent one of these 2 events : yes_isK (for Yes, is kicking) or no_isK.
We create these 2 local events in our t_collider Fsm and add 2 transitions, if is not kicking let’s go back to our checkCollision state, but if is kicking then go to our add force state and let’s kick this ball.
Don’t forget to make the handle collision transition points to the isKicking state, and not directly to the add force state anymore. Like this our add force action only triggers when we release our kick button.