Roll Over the Obstacle- Game Dev Series 96
Objective: create a rolling action to cross the obstacle.
We will create the rolling action in today’s article. Rolling action is quite iconic action in action game. It can be used to cross obstacles or avoid dangerous.
This action movement is also based on “animation play first, character move later” concept. You will need to import the animation to finish the following proceed.
Animation logic
The timing of doing rolling is very specific, we only need to do it when we are running, which is the good place to make an animation state.
We need a trigger to start rolling when running, create a trigger as parameter and assign it to the transition.
When rolling is finished, just let it transit back to Running after playing.
Now we can active the trigger in script.
Let’s roll
Same as grabbing on ledge, it would be less buggy if we disable CharacterController when we are rolling. To do the rolling, we can use left shift to start this action.
Create an if statement to check the input inside Player animation turning checking function.
We can do the rolling now. However, it would snap back to the origin position immediately. We need the animation state behavior script to fix the position issue.
Before we add the behavior script, let’s create an empty object to store the position after rolling.
Same as we locate the clime up position from ledge, duplicate Player to mark the position when after rolling.
Remove all the unnecessary components just keep transform. We will use this position in script.
Create a gameobject variable in Player script and assign the one we just created. Now we can create an animation behavior script.
As usual, get the reference of Player script in OnStateExit() of RollingBehavior script. Before that, we need to create a public method in Player script to handle repositioning.
Call this method from RollingBehavior script.
All set! Player now would snap to the position after rolling.
Fix opposite rolling issue
There is a tiny problem we need to fix. When we rolling to the right, everything works fine. However, when we rolling to the left, the snapping position still be on the right.
We need another gameobject to locate snapping position to the left.
Duplicate the gameobject and readjust the new one to a proper position.
Then we need to make the system checking which gameobject should use as goal position.
The easiest way to check the facing direction is using localEulerAngles, which we used to rotate Player model.
Use it in an if statement.
All done! Rolling action is perfect now!