Framework 1: Player Roll Animation
There is an purpose in making a character roll, either it is to dodge something or get under something. In this framework article we will focus on making the character roll underneath an obstacle that blocks the top halve of our player.
Prepping for the Roll:
Download and import the necessary animation from the Mixamo website. I found one that is called “Standing to Rolling” and I downloaded the animation with “In Place” to checked off.
Back in Unity, set the rolling animation rolling clip as “Humanoid” and duplicate just the animation state so you can save it into your animation folder.
This is the current settings for the Animation clip. I set the looping to be off, and Bake the transform rotation Y and ZX position. I also set “Original” for both the Rotation and Y Transform Position.
Drag the animation clip into the animator. The way I set up the transitions is to have the Idle > Rolling > Idle. I also set the Running animation > Rolling > Running.
I created a new Trigger parameter called “Rolling” which I set a condition when Idle > Rolling and Running > Rolling. I left the transition from Rolling back to the Idle or Running state as empty and made sure that “Has Exit Time” was checked. Therefore the transition would happen naturally and automatically.
Logic Behind the Roll:
What I want the roll mechanic to actually accomplish is to have the player roll under the box on top reaching from point A to point B. To do this, we will need to understand the logic behind this concept:
When the player reaches a collider held by the floating box, we will trigger an toggle that allows the player to roll underneath by pushing the button “Left Shift”.
Then we will also toggle an animation clip which simulates the rolling action. During this rolling animation, we also toggle off the Character Controller. (This process is very similar to the Ledge climb mechanic) We also add in two empty game objects which will be our target destination. After the rolling animation is finished, we will re-enable the character controller and also snap the position of the character controller to one of the empty game objects position.
Scripting the Roll:
There is going to be some back and forth when creating the Rolling script because there is a lot of script communication between the Player script, the Rolling Collider script and Rolling Animation Behavior script. Start by creating a new C# script called “Rolling Collider” and attach this script to the floating box game object.
Rolling Animation Behavior Script:
Since we are following the same principles as the Ledge Climb Mechanic, we will be working with Animation Behaviors. Start by creating a new Behavior script that belongs to the Rolling Animation state. (make sure to have the correct state selected).
Just like the Ledge climb, we are going to open up the Exit section of the Behavior script, and just like the Ledge climb script we will be needing access to the Player script (which is also an parent gameobject). The public method which we are referencing hasn’t been created yet so we will need to go to the player script and do so.
Back in the Player script, we need to create a new public method which the Behavior script can access.
Then in the section which calculates the player movement, we are going to add in a new script which checks for “Left Shift” input.
We can also add some conditions before we invoke the rolling animation and that is to make sure the character isn’t jumping and that the player is colliding with floating box.
In this script we need to create some new variables, one boolean switch to check if we can roll, and one boolean switch for when we have collided with the floating box.
Next we are going turn off the Character controller and trigger the rolling animation. Also set the rolling boolean to true but make the collider boolean set to false.
Create two new game object variables that will be the two destinations points after the rolling animation has been completed. We want two destinations point because we also want the player to be able to reverse back.
Finally to finish up the Player script we are going to complete the public method which the Rolling Animation Behavior is going to access. We are going to use an IF and ELSE statement because we want to be able to roll left and roll right, we need to calculate the which side the player is facing. If the player is left, the Y rotations is automatically set to zero, therefore we will set the final transform position to “Finished Location”. If the player isn’t facing Left then the ELSE statement stands true.
At the same time we want to be able to re-enable the Character Controller and set the rolling boolean back to false.
The Float Box Collider Script:
Now it is time for the Box collider script. Basically we want the player to roll ONLY when the player colliders with one of these floating boxes, otherwise having the character roll is pointless.
Similar to how we script the Ledge behavior, we will follow the same setup. By using an On Trigger Enter method, we are able to check when the player colliders. Then we get access to the Player script so we grant the player script the ability to trigger the rolling mechanic.
We are also going to pass in two game objects called point A and Point B, and these two game objects are the empty game objects we had created earlier during setup.
Back in the player script, we are going to finish everything by adding an public method called Start Rolling which the Box collider script needs access too. In this method, is where we also set the “can Roll” boolean to true (since by default it is set to false). We are also going to assign the two game objects a name in the player script so the player script can locally access them.
Although this is a bit confusing, the main thing to understand is that the Player needs to collide with the box before the player can toggle the roll. Once the player has made contact, the collider script is going to toggle the player “can roll” boolean which allows the player to press the “Left Shift” button. Once the button has been press, the Character Controller turns off, and the rolling animation starts. Once the Animation just finish, we are going to enable the Character controller, and also tell the character controller to snap into a new position.
This is all smoke and mirrors when creating an rolling mechanic such as this.