Having troubles animating objects and materials to be used in the BGE?
How do I animate (and control) materials and objects in the BGE?
This was a question I recieved some days ago by my interns, and to be honest I never thougth about animating materials recently in the BGE, at least I did not in my past projects, although I already knew that it is possible but the fact that blender automatically names the animations makes a bit hard at first to understand, and also when you are working with more animation layers in the logic bricks to handle different animations. So that’s why I wrote this post about material animation in BGE.
So, let’s start with something very specific, we want a material that just perform animation in the color and scale, that’s all. The procedure will be like this:
- Select the default cube and add a material if not having one
- Go to frame 1 in the Timeline
- Go to Diffuse panel in the material tab and press RMB (right mouse button) and select “Insert Keyframe” in the pop-up menu.
- Go to frame 20 in the time line.
- Go to Diffuse panel and change the material color (green color, for example) and press RMB and select “Insert Keyframe”
If you are new to blender, some confussion might be happening here, so, lets be more clear in this part. To change the material color just locate this area in blender (called “Properties Panel”) wich contains much of the tools, but also you can find here the “Material Editor” as shown in the following picture:
Just by putting your mouse in the color selector and pressing LMB or RMB (left mouse button or right mouse button) you can change either color and set a new keyframe.
If you move the timeline will notice that the color changes, so the animation in material is done! Now we will animate some scaling in the cube (geometry), and the procedure will be like this:
- Go to frame 1
- Select the cube and press I-key while holding the mouse in the 3D view area (keyboard i to bring the keyframing “pop-up” window)
- Press “Scaling” option to set the scaling keyframe.
- Go to frame 20
- Press S-Key to scale the cube, just a little not too much.
- Press I.Key and select “Scaling” to add the keyframe.
Now go back to frame 1 and move the slider back and forth to see the animation. the cube will be changing the scale and the color also.
To sum up, all the procedure will look like this:
Controlling the animation with Logic Bricks
Next step is to add some logic bricks to control the animation, but what are the function of the Logic Bricks? The blocks (or “bricks”) represents preprogrammed functions which can be tweaked and combined to create the game/application. The system is broken up in three parts: sensors, controllers and actuators. Sensors sense when things happens, such as a collision, a key press, mouse movement. Sensors are linked to controllers, which compare them and activate actuators. Read more here about logic bricks.
You can load the logic panel by either, changing the layout panel or by changing the window
Got it? Well thso the procedure is going to be like this:
- With the cube selected press change the screen layout to “Game Logic”
- In the logic area press the “add sensor” button in the “sensors” section and select “Always” sensor.
- In the actuators area press “add actuator” and select “action” actuator.
- Drag a line from the conector in the actuator to the sensor con make a link between them, it will automatically add the “and” controller.
- In the Action actuator select the name of the object animation and set the animation to “ping pong” from frame 0 to 20.
- Add another “action” actuator and do the same, but this time select the material animation, also set it to ping pong and from frame 0 to 20.
Just in case you are wondering where to see the name of the animations, just go to the “Outliner” and go trough the object content and look for “animation” and cube elements, just like in the following picture:
As you can see, you can rename it so you can use your own naming convention for your projects. If there is no an outliner window, do not worry! and remember that you already know how to change the window type in blender (look two images above)
And well.. the whole process of adding the logic bricks looks like this:
Now press P-Key to activate the game engine (Make sure “Blender Game is active, not Blender Renderer” in the top center window), and you will notice that just one animations showns up while running the game engine, and this is because both animations are handled in the same layer (look for it in the Action actuator).
And that’s all! To sum up, every animation need to have its own logic brick that calls the actual animation (material or objecs transform, scale, etc) Now you also learnt that blender automatically makes names of every animation, but they are hard to locate if you do not rename it to make it more understandable or to relate to some object to make it more easy to locate.
And of course you can animate almost everything in the material editor, so you can do almost any kind of material animation here. The blend files is very basic and simple, but just in case you need it, you can grab it from here.
Hope you like this and find useful