(this recipe is part of the footvolley game menu )
00:00 Creating a sphere Collider around player
We create a child gameObject under the player, name it “t_collider” (in my personal convention t_ stands for trigger).
We add a 3D sphere collider to it and we set it as a trigger. The sphere symbolize the area where the player is able intercept the ball.
We detect whenever the ball enters the sphere and if the player shoots when the sphere collide the ball we shoot the ball away (Well, not exactly but we’ll see more elements of the gameplay later).
00:32 Adding a Sprite Component to visualise the interception area
3D sphere collider isn’t visible in game view, so in order to show the player its actual range of interception we’ll add a circle sprite hint on the ground. We import our aim circle ( a simple green circle with an alpha channel on it for transparency) into our images asset and set it as a sprite.
We add a sprite component to the t_collider gameObject, use the aim image as sprite.
But in order to get it visible in this scene we have to select its sorting layer we select the character layer, as it’s displayed above the ground one. Now as in every 2D Objects in 3D worlds, we’ll rotate a little by 90° the gameObject to make the sprite fit as if it’s a circle displayed on the ground (we don’t mind about the sphere collider rotation, as a sphere it can rotate forever without we notice it, if we were playing with a cube collider, we would make the sprite as a child object of the sphere collider, and then rotate it).
01:36 Adjusting sphere to sprite
Now the sphere is bigger than our sprite circle, let’s adjust the radius of the sphere collider (in our exemple a radius to 0.2 fit the circle).
Once it’s adjusted, it’s now our field of interception is a little short. Let’s make it more easier for the player to intercept by default by enlarging the scale. Just set the t_collider X & Y scale by 5.
02:02 Handling collision with playmaker
Yes, we can handle simple collision with a little few lines of code. But for our game, we’ll need more than that.
We need some FSM to detect when our player is kicking, is sliding and many other states.. With playmaker we’ll have to code less and gain some more time, so let’s see how.
00:12 Adding FSM state to the collider
Time for adding an FSM to some game Objects now.
On our t_collider gameObject, go into the FSM window (remember the lesson about setting up our windows), and right click on it to add a FSM. Once you’ve set a FSM you get the little red icon from hutonggames displaying next gameObject having FSM on it. Notice that a PlayerMakerGUI was created on stage. This is a needed component.
Let’s call the first state as Setup, it’ll save you some troubles to have a setup as a start state. Even if there’s nothing on it.
Now once the we finish to set up things (for the moment, nothing), we move to our idle state. The idle state is when we do nothing, but it’s a state that will constantly check for our next moves and triggers the next state accordingly to our actions.
03:16 Checking collisions
So right click and add another state called “checkCollision”. To pass from Setup to CheckCollision, we need a transition, an event to pass from one state to another. Here it’ll be the Finished event, basically it tells that once the setup actions are finished, go to the idle state.
Right click on the setup state /Add transition/Finished. The exclamation mark tells us that we’re missing something: to tell which state to switch once its finished. Just let the left mouse pressed from finished to our CheckCollision state to see an arrow link these two states.
Go to the CheckCollision state, and on the State tab click on the Action browser button on the below to display the action window. On it you can see all possible actions, let’s choose TriggerEvent.
The triggerEvent let you select a trigger mode, we want an OnTriggerStay behavior here. We’ll explain why later. We have now to choose a collide tag, it would be nice to activate the trigger only if our circle collides with the ball and nothing else. So create a tag Ball in the soccer ball object and let use it.
We need now to sent an event : once the circle collides with the ball, what could we do? let’s create a handle_collision event that will sent us to another state. Don’t forget to add the handle_collision to our state by clicking Add transition/handle_collision on our state. Then create another state called “Add Force”, link them.
If we let things that way, we’ll only check for one and only collision. Follow the logic : we loop to check for a collision, once we a collision occurs we “add force” to the ball (just a debug test here). And then what? it’s over. So we have to say that once the test had been executed, go back to the loop that checks for collision.
So we add a onFinished transition, and then link it to the checkCollision state. And the loop goes on.
Just for test, add “DebugLog” to the Add Force actions, don’t forget to check “sent to unit log” to see it in the console.
But before doing any testing, you have to know that objects can only collide to triggers if one of the 2 tested objects got a rigidbody attached to it. So let’s attach a rigidobdy to the ball, then test.
Everything collides well, so next on the gameplay !