Blender 3D: Noob to Pro/An aMAZEing game engine tutorial

Introduction
This tutorial is intended as an intermediate introduction to the Blender game engine, in the form of a game, and is the sequel to Platformer: Creation and Controls. It will require a familiarity with the Blender UI, simple commands (such as AKEY to select) and basic modeling skills. The game we will create within Blender will have the following features:


 * 1) a protagonist controlled by means of the WASD keys
 * 2) a maze surface without walls
 * 3) death on falling off the maze
 * 4) multiple levels
 * 5) dynamic obstacles
 * 6) a goal within the maze that will transfer you to the next level

This tutorial was written for version 2.44 and has been tested for all later versions (as of April, 2008)

Maze Surface
Now we'll make the maze. When you are creating the path of the maze surface keep in mind that you need a start and a goal.




 * 1) Clear the scene; Select all (AKEY), delete selected items (XKEY &rarr; ENTER)
 * 2) Go into top view (NUM7)
 * 3) Add a grid (SPACE &rarr; Add &rarr; Mesh &rarr; Grid) with X and Y resolution of 16
 * 4) Scale grid by 24 times (SKEY, type 24, ENTER)
 * 5) Enter edit mode with the grid selected (TAB)
 * 6) Go into face select mode (CTRL+TAB &rarr; Faces)
 * 7) Start selecting faces so as to make a two dimensional maze (use BKEY to draw selection boxes)
 * 8) With the faces selected, duplicate those faces (SHIFT+D, RMB)
 * 9) Move those faces away from the original grid (GKEY or the movement wiget CTR+ALT+GKEY)
 * 10) Delete original grid
 * 11) Center your maze using GKEY or by pressing the Center button in the Mesh panel of the Editing buttons (F9)

You should now have a two dimensional maze. In the game engine, we only see one side of every face, so this maze will not appear to be there when seen from below, and will be obviously two dimensional when seen from the surface of the maze itself. This is undesirable so we will make our maze 3 dimensional by extruding the maze surface down.




 * 1) Go into side view (NUM3)
 * 2) Select all the faces (AKEY)
 * 3) Extrude down, -1 unit (EKEY &rarr; Region), hold CTR to constrain to 1 unit increments, or manually type in the value -1, like you scaled the grid in the beginning.
 * 4) LMB to accept the operation, or RMB to cancel the move of the faces.  If you cancel it, the faces will have extruded, but will simply not have moved.  You can undo the operation (CTRL+Z), or delete the vertices/faces you have extruded (WKEY &rarr; Remove Doubles).

Your maze is now complete, only lacking someone trapped in it...

Character


We will create a very basic character sufficient to illustrate the concepts meant to be conveyed in this tutorial. You can make a character as complex or basic as you desire, but it is important to always have an indicator of direction on your protagonist, otherwise the player may become confused. So, we will use a primitive with a 'nose' (i.e. a face or vertex extruded away from the object center in order to provide a point of rotational reference). I'll use a cone for this tutorial, simply because they don't roll very easily.




 * 1) Enter top view (NUM7)
 * 2) Add the cone (SPACE &rarr; Mesh &rarr; Cone, use default values)
 * 3) Select cone and enter Edit mode (TAB)
 * 4) Enter face select mode (CTRL+TAB &rarr; Faces)
 * 5) Select a face on the side of the cone
 * 6) Extrude it about 1/4 of a unit or less (EKEY)
 * 7) Merge the extruded face to make the nose pointy (WKEY &rarr; Merge &rarr; At Center)
 * 8) Enter object mode (TAB)
 * 9) Rotate the cone so the nose is aligned with the positive Y axis (RKEY, align with the green arrow)

You've got your character now (Not exactly Pixar-like, but it will do for now). Now we just need to give it motivation.

Note: From points 8 to 9 it would seem that the cone should rotated so that the “nose” is aligned with thee Y-axis in object mode, but after doing this I encountered the cone still moved in the original Y-axis direction (before rotation). To remedy this I did the alignment of the “nose” to the Y-axis in edit mode instead, this seems to have solved the problem.

Motion
We'll now make the character move, via the use of the WASD keys. In order to do this, game logic needs to be associated with the WASD keys. Copy the configuration seen in the screenshot, taking care to include the convex hull polytope collision in the top left as well as depressing the 'L' buttons, which make the force and torque local to that object rather than global. If the force was global, the protagonist would continue moving in a straight line, even when turning.


 * 1) Go to the Logic buttons (The purple pac-man icon or F4)
 * 2) Add four Sensors, Controllers and Actuators.
 * 3) Maximize the logic window (CTRL+UPKEY)
 * 4) Copy the configuration seen in the screenshot below.



(Note: I made the simulation using Actor->Dynamic since there was no way to use both dynamic and rigid body in 2.49b. After some testing it appears to work correctly.)

dLoc/dRot vs Force/Torque

The character logic in this tutorial is very similar to that of the previous platformer logic, but it differs in that it uses force and torque rather than dLoc and dRot, respectively. The difference between these two is that force and torque move and rotate the object within the bullet physics engine, applying friction and collision to the process, whereas dLoc and dRot move and rotate the object without regard for any other 'actors' (objects factored in to the physics engine's calculations). Although dLoc and dRot would serve in most maze games, many other instances demand force and torque. However, most of these instances occur in more complicated games with a multitude of interactions with dynamic objects. Later in this tutorial we will add dynamic objects to our maze, and the reasons for using force will become clear.

Testing
Now the time has come for the first test of our game.


 * 1) Add a light source well above the maze (SPACE &rarr; Lamp &rarr; Lamp), align in front view (NUM1)
 * 2) Press NUM5 to enter perspective viewmode, which gives a realistic view, rather than a view in which objects stay the same size with distance. Be sure to switch back to orthographic view when you are editing (NUM5)
 * 3) Enter textured mode (ALT+ZKEY) -- press ZKEY to switch back to solid view mode
 * 4) Switch into top view (NUM7) or camera view once we install the camera (NUM0)
 * 5) PKEY to play the game
 * 6) ESC to escape testing the game

If you receive a "No camera" error, disregard it. We'll be adding a camera later.

If your character doesn't move at all make sure you drew the connecting 'wires' between the actuators in the logic panel, as well as setting the "Actor" "Dynamic" and "Rigid Body" buttons to True.

You should now have a nosy cone that speeds around the maze.



(Noob Question 1: My cone likes to rotate without me pressing A or D. It also likes to find hidden/nonexistant bumps on the maze surface. How do I fix this? )

(Answer to NQ1: If I understand your question right. If i used torque to turn and force to move forward and pressed forward then turned it started spinning out of control. My solution was to use rotation instead of torque.)

Noob question 2: The cone just goes through the maze. How do I make the maze block the cone?

Answer to NQ2: First make sure your cone is not even partially inside the maze before starting the game engine. Secondly, select your cone (named "Protagonist" in my example), then go back to the "physics" properties in your property window. If you do not see the property window press the NKEY and look on the right of your screen. Under "Physics type" make sure to have selected in the scroll down menu "Dynamic" or "Rigid body". Then, scroll down to "collision bounds" and check the box. You shall also select a shape from the "bound" drop down menu, for example "cone".

Falling
We will now implement the "death on falling off maze" feature. In order to accomplish this, we'll add something that allows us to check whether the cone has fallen off the maze or not. The simplest way to do this is with a 'skydome' object the entire maze is within, and although there are a number of more elegant ways to accomplish the goal, we will use the skydome method for its simplicity to replicate. From now on we'll need to test the maze with both layers one and two enabled (SHIFT+2KEY)


 * 1) Add a new cube
 * 2) resize it to gargantuan proportions (SKEY, just large enough to encompass the maze, and all within it)
 * 3) Facultative: Move it to layer 2 (MKEY, 2KEY, Enter)

Now that you have a massive cube, go to the logic panel and add one Sensor, Controller, Actuator set, and set them as seen in the screenshot.

This restart the scene if the protagonist (and only the protagonist, hits the floor) This will let the dynamic obstacles, which are added later on, fall through and not get stuck there!
 * 1) Sensor: Touch, under "property" put the name of your character (here "Protagonist")
 * 1) Controller: And
 * 2) Actuator: Scene, restart
 * 3) Properties: Physics (last icon on the left), enable actor button as well as "ghost" and "invisible"



If you test you game now, you should see the game be restarted when you fall off the maze. As we discussed earlier, faces are one-sided in the game engine, so you should not be able to see the cube.

(Newbie Question 1: When I did this, my cone just kept on falling. When I moved the cube back to layer 1, it worked fine. Am I missing something?) (newb note: ok i had the same problem every time i was just in layer 1 but when i was just in 1 then i just kept falling)

(Answer: The tutorial says to create the cube in layer 2, although the layer shouldn't matter. Make sure you test the game with both layers one and two selected. Also double check the logic for the 'skycube')

(Answer2: I had the same problem. I fixed this by replacing the cube with a grid, scaled and placed under the maze. The grid had the touch logic mentioned above.)

(Answer3: Same here. I thought I'd remove "Protagonist" from the material reference, but that wouldn't do if the bricks fell down and touched the cube, it'd just restart the level... the solution: create a property in the cone, a bool value named "isCone" (=true), then in the cube add a sensor 'collision' that checks for the object's property 'isCone' and keep the rest as shown.)

(Answer4 (to restart when block hits cube):Do all as the pic shows you and RMB the cone and go to links and pipelines and rename the MA: to cone or whatever you want, then RMB the cube then in the Touch sensor then rename the MA: to whatever you called the other MA: . :D this is in 24.9b)

(Answer5: For blender 2.57a you need to place a plane or something under the maze. Then give it (touch -> and -> Scene(restart)) game logic. If it isn't working make sure that actor is selected under physics tab.)

(Question 2: How do I make the box translucent? Right now all I see, also when testing, is the skybox. Also, when I put the box underneath the maze, it works fine, but then none of the objects have shading, while when not showing layer 2 everything looks shaded. Can anybody give a solution? This is in 2.48a)

(Answer: To make something transparent- press F5; press the "Material Buttons" button; under the Links and Pipeline tab press Halo and ZTransp, unpress Shadbuf; under Material make A 0.000.)

Killing Floor
To add tension to your game, you can give motion to the floor. It will rise with time!
 * 1) Add a static Plane, and set its logic panel as described above.
 * 2) Add an ALWAYS sensor and connect it to a AND controller and connect it to a MOTION actuator that has a vertical linear velocity of 0.01 (along the z-axis).



This way it will slowly come up. To make it evident to the player that the floor is rising, change the color of it (for example blue for water) and make it visible again.

You've just added some tension to your game! If you run out of time, you will touch the floor and restart! If you need more time, simply put a lower vertical linear velocity.

Camera
SPACE &rarr; Camera near the protagonist cone. There are two easy ways to do the camera: logic camera and child camera. In this case you might be better off with the logic camera, but it really is a matter of personal preference.

 Logic Camera: Add to the camera's logic panel   Child Camera: Select the camera, then select the protagonist (order is important) then CTRL+PKEY to parent the cone to the camera. Align the camera in a view that you like, and test the game.   First Person Camera: Well, this one doesn't really count as another method, but if you're willing to redo your motion to just move the camera instead of the character, (just do everything you did for the character for the camera) you can run around in the first person. Or you can just put the camera in the cone because one of the sides always are invisible (look in texture mode) and parent it to the cone. REMEMBER: To access the ingame camera during gameplay, before you start, hit ctrl 0 or you can go to (view > camera)  
 * 1) Sensor: Always
 * 2) Controller: And
 * 3) Actuator: Camera, to object "Cone" (or whatever your protagonist is called--look under the object panel when you have the protagonist selected) height 5, min 5, max 10.

(Newb Question 1: No matter what I do (even delete the camera!), when I hit 0 or ctrl+0, I always see either only the death plane (I used a plane below the maze instead of a cube around it) or nothing, depending if I have layer 2 selected or not. There's only the one camera (or none, if I delete it), according to the Oops Schematic. What's wrong?)

(Answer: Ctrl+0 assigns the selected object as a camera. To solve your problem select your camera and press ctrl+0, to enter camera view press 0)

Beautification
Blender is a great 3D modeling program, and with such a vast number of tools at your disposal, you should be able to make your game look better than this: We'll add one or two textures to each of our objects.
 * 1) Select the object you want to texture
 * 2) Go into edit mode if you want your object to have more than one color
 * 3) Select all the faces you are going to assign to a color (CTRL+TAB → Faces)
 * 4) Go to the edit panel (F9) on the buttons window
 * 5) Near the left there should be a panel that says "Material". Click on the buttons highlighted in the screenshot
 * 6) Adjust the color of that material by clicking on the white field under "Diffuse". A color picker appears. Pick a color you like!



Mist
It looks better now, but when the camera is nearly horizontal, you can see where the camera stops rendering the maze. You can fix this by selecting the camera and, in its edit buttons, changing its clipping range... but there is a better way. Mist.



Mist obscures everything a certain distance away, is the same color as the world texture, and can be handy. We'll add some mist to our game so the player can't see too far ahead of him, thus making the game too easy.

We'll make the mist quite close, although you can vary the distance. Go to the Material button, and to the World sub-buttons, then copy the settings in the screenshot. When you test this out, make sure you are in texture mode to see the mist.



(Question: I copied the screenshot but i can't get the mist to work. What's going on?)

(Answer: Change Start to a smaller value like 15.)

(Question: Is the screenshot correct, or are my sizes different. I found that a mist distance of 10 was barely visible, yet a distance of 1 had a much better effect. Is this normal?)

(Question: I have been able to create the mist, and can see it when I start the game, but the background colour is still grey, so the maze is obscured but everywhere else is still grey.

Levels
Now all that remains is to create the next level with some obstacles.
 * 1) Add a cube at the end of the maze, just above the floor level so the cone can touch it (SPACE → Add → Mesh → Cube)
 * 2) Press the Scene dropdown menu and select "ADD NEW" → Full copy
 * 3) You can now go back to the Maze Surface section to make a new maze mesh
 * 4) Go back to level one via the Scene menu
 * 5) Select the End of level cube
 * 6) Change its logic panel to resemble this:


 * Sensor: Touch, coneMat
 * Controller: And
 * Actuator: Scene, Set Scene, Scene.001 (or whatever you named your level 2)

(noob: When I went into level 2 my player name changed to coneMat.001 and I had to change all my goals and the giant restart cube assignments.) Answer: Rename the coneMat.001 to whatever you had the material named for the first scene - this fixed it for me.

(noob: In my level 2 my cone hits the cube and nothing happens!) Answer: Make sure that the Sensors, Controllers and Actuators tabs under the Logic panel (F4) are connected with wires.

(noob: When I went to level two I can't see anything!

To make it so multiple objects have to be touched (or acquired by the character, as it may be), give each object the logic settings described above and change the actuator type to Message with the subject "goalget". If you'd like the object to disappear after it is collected, add an Edit Object actuator to the object of the type "End Object", and link it to the existing AND controller.

Next, select the massive cube that restarts the level upon contact with the character. If you chose not to include one such cube, create an empty at the start of the level (so you can easily find it again later). Give the cube or empty the following logical operators:


 * Sensor: Message, goalget
 * Controller: And
 * Actuator: Property, Add, wincon, -1


 * Sensor: Property, Equal, wincon, 0
 * Controller: And
 * Actuator: Scene, Set Scene, Scene.001 (or whatever you named your level 2)

Then click "Add Property" and give it the Type Int, the Name wincon, and a value equal to the number of objects needed to be collected to complete the level (if all of them, make it equal to the number contained in the level). This can be expanded to include other multiple win conditions besides the collection of an item by making the completion of each condition subtract from "wincon".

You now have a multilevel maze game. Feel free to elaborate on the gameplay mechanics and models so they look better.

''Noob note: I typed in where i thought everything should go but it's not doing anything. I'm not sure of what I'm looking for while in playmode.''

Dynamic obstacles
This part of the tutorial will assume increased skills and knowledge of the Blender interface and basic commands. As such, this section will be written less specifically, and the shortcuts for all basic commands will not be covered here. From this point on, this tutorial is of an intermediate level. If you cannot follow this part of the tutorial, you can learn the basic functions used below in an earlier tutorial.

We will now add some dynamic obstacles. In this case, they will constitute a 'brick' wall that can be broken through by your character.


 * 1) Add a cube
 * 2) Scale it (in edit mode) to the dimensions 1,0.5,0.5
 * 3) Bevel it (WKEY) recursion 1, size 0.03 (press space to enter value manually)
 * 4) In logic panel, set it to Actor, Dynamic, Rigid body, Mass 0.5
 * 5) Go right below that to click Bounds and leave it on "Box".  That makes Blender realize that what you added is a box, and should act like it (instead of rolling, it will slide now)
 * 6) Move it to an intersection in your maze (GKEY) and duplicate it until you have a brick wall (ALT DKEY, LMB)



You now have a barrier that your character can break through.

Conclusion
You have now learned enough of the Blender Game Engine (BGE) to create your own game. If you wish to go very far into Blender games, I recommend you learn Python, as trying to make a full game within the graphical interface for the BGE is like trying to dig a grave with the blunt half of a toothpick. Blindfolded. And the toothpick is glued to your forehead. However, you are now well on your way to becoming a game-maker! For a basic beginner tutorial about Python, "Open Source Video Game" series.

If you have any questions, concerns, or 'noob notes', please post them in the discussion page for this tutorial, and not in the tutorial itself.