(this recipe is part of the footvolley game menu )
This post is about creating and tuning the footy volley gameplay, we’ll go back and forth just to test elements of gameplay.
00:00 Making the collider disappear
Let’s apply our first idea : make the collision circle appears only when we press on our kick button.
The player is at the center of this idea, so let’s select it and create a FSM on it by right clicking Add FSM on its playmaker window. We set up as usual the setup state (good habit), and then we make the transition to another state : Idle .
This Idle state will be very useful to handle the inputs, the anims and the initialisation when our player “come back to normal”; So our first idle state action will be to deactivate the t_collider gameObject .
Go into the action window, select the Activate game object action, and specify which gameObject to deactivate by drag & drop ou t_collider on it. To tell that we want to deactivate it, we simply uncheck the activate option. (if it’s checked it means the gameObject is active, if unchecked it means its inactive. quite simple).
Set the recursive option to true, it means that the action will be applied to the children of the gameObject that we’re working on it.
If we test it right now, we should see that the circle has disappeared..
01:19 Detecting shoot button press
Now we’ll make our circle appears while we’re pressing our shoot button. first things first, let’s set up which button will it be.
Go back to our player/Idle state , then add a new action : Get Key Down (we use the keydown event instead of keyup because we’ll have a gameplay mechanic later that will require to have button pressed for a certain time).
Then we choose the space key, and we have to select which event to sent if the spacebar is pressed.
So we’ll create a new one in the Events window, the kickPressed event (yeah, i’m that terrible when it comes to create variable names).
01:57 Creating the isKicking state
(I’ve told you that i’m awful at creating names)
We’re now make a transition to a new state: isKicking.
We create a transition from the Idle state to the isKicking state by the help of our kickPressed event.
If the player press the space key, so he’s trying to kick the ball, let’s make our circle appears. Create again an Activate Game Object action, choose again our t_collider, then we check the activate option.
By the way let’s add another good habit here : renaming our actions. If we keep like this we’ll see dozen of actions getting the same names (activate game object, getkeydown..) and we’ll loose focus; so let’s rename it in Activate collider, that will be understandable at a glance like that.
03:27 Stop Moving
We want now that our player stop moving while shoot button is pressed. The best way to do that is to create a boolean variable called canMove that’ll be set to true when we’re not pressing the shoot button, and false while pressing the button.
03:32 Setting up boolean value
In the FSM window, we go to variable tab, then we create a bool value canMove.
In our Idle state we create the action set bool value. We check the option bool val , checked means true, unchecked false. And for clarity we rename our action in canMove : true. So now when we’re in idle state, canMove is true.
We have now to tell that when we’re in isKicking state, we cannot move. Instead of creating action, this time we can copy/past previous action. In our canMove : true action, we just click on the option button on the right, select copy selected action, then in our isKicking state do the same and paste our action.
from now we just uncheck the bool value to turn it to false, and of course rename our action into canMove : false for our own mental sanity.
05:18 Modifying controller script
Now onto the code.
remember our Player controller script? it’s time to adapt it a little.
using HutongGames.Playmaker; //will allow us to use the playmaker types like FsmBool
private FsmBool canMove;
public PlayMakerFSM; //which contains all FSM references
//now we have to ‘link’ our MyFSM var to our current player FSM
//we do this into the Awake() function, which is called before the start() one.
MyFsm = this.GetComponent<PlayMakerFSM>();
//once everything’s set, we can link our FSM values to local vars
//FindFsmBool must contains the exact name as set up in our FSM window.
canMove = MyFsm.FsmVariables.FindFsmBool(“canMove”);
//then in the update, we’ll only move the player if the canMove bool is set to true
//note that you have to add .Value to get the stored values of any FSM variables
//code to move player
08:32 Getting back to idle
It’s okay, our player stop moving when we press space..but isn’t moving at all once we release space key too! It’s not a big deal, we can do a backward step quite easily.
If we used the GetKeyDown to enter a state, we’ll now tell to go back to our idle default state once the space key is released.
So in our isKicking state, we add the action GetKeyUp, we choose the space key to listen and we create another event : kickReleased. We set up the getKeyUp action to fire up the kickReleased event. Than what should we do if this event is fired? We should tell to go back to Idle State. Let’s do it by right clicking on isKicking state, then Add_transition/kickReleased, and link it to Idle State.
So everything’s back to normal : the idle state tells to disable our collider, sets our canMove boolean to true, and listen to the shoot key to be pressed.
09:32 Sent back ball when colliding the ball
In our precedent step, we finished by testing collisions between player and ball with a log info.
We now sent back the ball to the other side (no matter where at the moment, just playing).
Select the t_collider object from our player, we’ll change its FSM in the playmaker window.
09:42 Adding force to the ball
We now go back under our soccer ball FSM.
Go to the Add force state, and delete our log action, time to make the ball fly.So, in the action window select the add force action.
We choose the object that will be exposed to our impulsion force : the soccer ball object.
Then we apply to it our desired force mode , here an impulse force that will make the ball fly in a wanted direction.
Finally we set (for start) manually the direction we want to make the ball fly : 0.3 by X coordinates (makes the ball roll in the other side direction), and 0.3 by Y coordinates (makes the ball fly a little in the air).
Test it and voila ! We can push our ball a little.