Animation in the Blender Game Engine
Blender 3d tutorial
If you want to bring your game to life, it is absolutely necessary to animate the objects in it. To use your animations in a game in Blender 3D, we will create a basic animation for a moving platform and use various Game Logic Bricks to use that animation in the game engine. First we will have the animation continually repeat, then we will learn how to only have the animation activate on command.
To start us off, I have created a blend scene we can use. This builds off of the first person movement tutorial and includes the same basic movement and jumping. This should be more than enough for our needs in this tutorial.
If you have already followed the first person movement tutorial, you may also find that I have added a platform and an upper level to our scene. When we are done with this tutorial, we will be able to stand on the platform and gain access to the upper level by animating the platform with a key press.
Animating the Platform
To animate a object for the game engine, you animate in the same way you would animate objects for videos. Our animation is going to simply move our platform from the floor to the upper level.
To set the first position, select the platform object. Make sure you are on frame one in the timeline window, press I, and select Location. This is the first keyframe in our animation.
For the second position, go to Frame 50 and make sure the platform is still selected. Then move the platform along the Z-Axis by 3.5 (the height of the upper level). Press I and select Location in the popup menu.
If you play your animation, you should see the platform simply rise from the floor to the upper level.
In the Game Engine
With our animation complete, we can now move into the game engine. Switch the Layout of Blender to Game Logic. This setting is to the right of the help menu.
Before you start adding logic bricks, this is a good time to define the behavior of our platform elevator. When you create something in the game engine, we need to know how we want objects to behave. It's quite important because, even though this is a very simple animation, you can still greatly effect how the player uses it. In this tutorial we will go over two behaviors.
The first is a perpetual elevator, which will go up and down repeatedly and the player is expected to wait until the right time to jump on for a ride. The second is having the player press a key while they are standing on the platform to make the elevator go up or down.
As you can imagine, as a player, these require two very different approaches. One makes the elevator an obstacle for the player to get through, while the other is simply an interactive feature that the player can use.
To create the perpetual elevator behavior, select the platform. In the Logic Editor window, add an Always sensor by using the Add Sensor dropdown in the first column. Now turn on the True pulse setting, the left most button labeled with three dots. This forces the Always sensor to be repeatedly activated. Without this, our elevator would only move once, rather than up and down.
Now add an Action actuator using the Add Actuator dropdown in the third column. Then connect the Always sensor to this new actuator by connecting the dots. An And controller should automatically appear when you do this.
In our Action actuator, change Play to Ping Pong in the Playback Type dropdown, select the PlatformAction animation in the Action textbox, uncheck the Continue setting, and change End Frame to 50.00 (this is the last frame in the animation we created).
Finally, you can now test out your game by going into Camera View (Numpad 0) and pressing P. If you turn to your right (WSAD controls!), you should see the platform moving up and down.
At this point, you have learned the key to using animations in your games: the Action actuator. Now lets change the behavior of our elevator so that it only activates when we press the Shift key.
Because we have our Action actuator already set up for us, we don't even need to touch it. Instead, we only need to change the sensors, which are the bricks that trigger the actuators.
To use the Shift key, simply change the Always sensor to a Keyboard sensor. Then click on the Key and press the key you want to use to activate the elevator.
If you play your game now and press the Shift key (or whichever key you selected), you should see the platform animate. However, you'll probably notice that you can activate the animation from anywhere in the scene. Not cool! Let's change that in the next step.
The Collision Sensor
In order to require players to be on the platform before they can activate it means we need two sensors to be triggered before playing the animation. To do this, select the platform, and add a new Collision sensor and connect it to the existing And Controller.
This is where the And controller is important. Since it has two sensors connected to it, it means the Collision sensor and the Keyboard sensor need to be activated before the Action actuator will start.
If you were to change the And controller to an Or controller, then you would only need to activate one of the two sensors. Ultimately, this means you can connect all the required sensors needed to activate an actuator so that your animations and your game behave exactly how you need it to.
With that, I will end this tutorial. As always, if you have any comments, suggestions, or ways to improve this tutorial, I am definitely open to hearing them. Have fun!
No single tutorial will cover everything, so if you want to dig deeper, here are some resources for you to check out. If you have other resources you think I should add to this list, feel free to let me know!
- Collision Sensor, Blender Doc
- Keyboard Sensor, Blender Docs
- Action Actuator, Blender Docs
- Animation Keyframes, Blender Docs