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

| previous module: "Platformer: Creation and Controls" | • Table of Contents • Glossary | next module: "Platformer: Physics Fixes" |

## 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:
* a protagonist controlled by means of the WASD keys
* a maze surface without walls
* death on falling off the maze
* multiple levels
* dynamic obstacles
* 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.
* Clear the scene; Select all (AKEY), delete selected items (XKEY → ENTER)
* Go into top view (NUM7)
* Add a grid (SPACE → Add → Mesh → Grid) with X and Y resolution of 16
* Scale grid by 24 times (SKEY, type 24, ENTER)
* Enter edit mode with the grid selected (TAB)
* Go into face select mode (CTRL+TAB → Faces)
* Start selecting faces so as to make a two dimensional maze (use BKEY to draw selection boxes)
* With the faces selected, duplicate those faces (SHIFT+D, RMB)
* Move those faces away from the original grid (GKEY or the movement wiget CTR+ALT+GKEY)
* Delete original grid
* 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.
* Go into side view (NUM3)
* Select all the faces (AKEY)
* Extrude down, -1 unit (EKEY → Region), hold CTR to constrain to 1 unit increments, or manually type in the value -1, like you scaled the grid in the beginning.
* 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 → 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.
* Enter top view (NUM7)
* Add the cone (SPACE → Mesh → Cone, use default values)
* Select cone and enter Edit mode (TAB)
* Enter face select mode (CTRL+TAB → Faces)
* Select a face on the side of the cone
* Extrude it about 1/4 of a unit or less (EKEY)
* Merge the extruded face to make the nose pointy (WKEY → Merge → At Center)
* Enter object mode (TAB)
* 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.
* Go to the Logic buttons (The purple pac-man icon or F4)
* Add four Sensors, Controllers and Actuators.
* Maximize the logic window (CTRL+UPKEY)
* Copy the configuration seen in the screenshot below.
$_{(Note:ImadethesimulationusingActor->Dynamicsincetherewasnowaytousebothdynamicandrigidbodyin2.49b.Aftersometestingitappearstoworkcorrectly.)}$
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.
* Add a light source well above the maze (SPACE → Lamp → Lamp), align in front view (NUM1)
* 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)
* Enter textured mode (ALT+ZKEY) -- press ZKEY to switch back to solid view mode
* Switch into top view (NUM7) or camera view once we install the camera (NUM0)
* PKEY to play the game
* 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.
$_{(NoobQuestion1:MyconelikestorotatewithoutmepressingAorD.Italsolikestofindhidden/nonexistantbumpsonthemazesurface.HowdoIfixthis?)}$
$_{(AnswertoNQ1:IfIunderstandyourquestionright.Ifiusedtorquetoturnandforcetomoveforwardandpressedforwardthenturneditstartedspinningoutofcontrol.Mysolutionwastouserotationinsteadoftorque.)}$
$_{Noobquestion2:Theconejustgoesthroughthemaze.HowdoImakethemazeblockthecone?}$
$_{AnswertoNQ2:Firstmakesureyourconeisnotevenpartiallyinsidethemazebeforestartingthegameengine.Secondly,selectyourcone(named"Protagonist"inmyexample),thengobacktothe"physics"propertiesinyourpropertywindow.IfyoudonotseethepropertywindowpresstheNKEYandlookontherightofyourscreen.Under"Physicstype"makesuretohaveselectedinthescrolldownmenu"Dynamic"or"Rigidbody".Then,scrolldownto"collisionbounds"andcheckthebox.Youshallalsoselectashapefromthe"bound"dropdownmenu,forexample"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)
* Add a new cube
* resize it to gargantuan proportions (SKEY, just large enough to encompass the maze, and all within it)
* 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.
* Sensor: Touch, under "property" put the name of your character (here "Protagonist")
This restart the scene if the protagonist (and only the protagonist, hits the floor)
* Controller: And
* Actuator: Scene, restart
* Properties: Physics (last icon on the left), enable actor button as well as "ghost" and "invisible"
This will let the dynamic obstacles, which are added later on, fall through and not get stuck there!
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.
$_{(NewbieQuestion1:WhenIdidthis,myconejustkeptonfalling.WhenImovedthecubebacktolayer1,itworkedfine.AmImissingsomething?)(newbnote:okihadthesameproblemeverytimeiwasjustinlayer1butwheniwasjustin1thenijustkeptfalling)}$
$_{(Answer:Thetutorialsaystocreatethecubeinlayer2,althoughthelayershouldn'tmatter.Makesureyoutestthegamewithbothlayersoneandtwoselected.Alsodoublecheckthelogicforthe'skycube')}$
$_{(Answer2:Ihadthesameproblem.Ifixedthisbyreplacingthecubewithagrid,scaledandplacedunderthemaze.Thegridhadthetouchlogicmentionedabove.)}$
$_{(Answer3:Samehere.IthoughtI'dremove"Protagonist"fromthematerialreference,butthatwouldn'tdoifthebricksfelldownandtouchedthecube,it'djustrestartthelevel...thesolution:createapropertyinthecone,aboolvaluenamed"isCone"(=true),theninthecubeaddasensor'collision'thatchecksfortheobject'sproperty'isCone'andkeeptherestasshown.)}$
$_{(Answer4(torestartwhenblockhitscube):DoallasthepicshowsyouandRMBtheconeandgotolinksandpipelinesandrenametheMA:toconeorwhateveryouwant,thenRMBthecubethenintheTouchsensorthenrenametheMA:towhateveryoucalledtheotherMA:.:Dthisisin24.9b)}$
$_{(Answer5:Forblender2.57ayouneedtoplaceaplaneorsomethingunderthemaze.Thengiveit(touch->and->Scene(restart))gamelogic.Ifitisn'tworkingmakesurethatactorisselectedunderphysicstab.)}$
$_{(Question2:HowdoImaketheboxtranslucent?RightnowallIsee,alsowhentesting,istheskybox.Also,whenIputtheboxunderneaththemaze,itworksfine,butthennoneoftheobjectshaveshading,whilewhennotshowinglayer2everythinglooksshaded.Cananybodygiveasolution?Thisisin2.48a)}$
$_{(Answer:Tomakesomethingtransparent-pressF5;pressthe"MaterialButtons"button;undertheLinksandPipelinetabpressHaloandZTransp,unpressShadbuf;underMaterialmakeA0.000.)}$

## Killing Floor

To add tension to your game, you can give motion to the floor. It will rise with time!
* Add a static Plane, and set its logic panel as described above.
* 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 → 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
** Sensor: Always
** Controller: And
** 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.
* 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)
$_{(NewbQuestion1:NomatterwhatIdo(evendeletethecamera!),whenIhit0orctrl+0,Ialwaysseeeitheronlythedeathplane(Iusedaplanebelowthemazeinsteadofacubearoundit)ornothing,dependingifIhavelayer2selectedornot.There'sonlytheonecamera(ornone,ifIdeleteit),accordingtotheOopsSchematic.What'swrong?)}$
$_{(Answer:Ctrl+0assignstheselectedobjectasacamera.Tosolveyourproblemselectyourcameraandpressctrl+0,toentercameraviewpress0)}$

## 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.
* Select the object you want to texture
* Go into edit mode if you want your object to have more than one color
* Select all the faces you are going to assign to a color (CTRL+TAB → Faces)
* Go to the edit panel (F9) on the buttons window
* Near the left there should be a panel that says "Material". Click on the buttons highlighted in the screenshot
* 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:Icopiedthescreenshotbutican'tgetthemisttowork.What'sgoingon?)}$
$_{(Answer:ChangeStarttoasmallervaluelike15.)}$
$_{(Question:Isthescreenshotcorrect,oraremysizesdifferent.Ifoundthatamistdistanceof10wasbarelyvisible,yetadistanceof1hadamuchbettereffect.Isthisnormal?)}$
$_{(Question:Ihavebeenabletocreatethemist,andcanseeitwhenIstartthegame,butthebackgroundcolourisstillgrey,sothemazeisobscuredbuteverywhereelseisstillgrey.}$

## Levels

Now all that remains is to create the next level with some obstacles.
* Add a cube at the end of the maze, just above the floor level so the cone can touch it (SPACE → Add → Mesh → Cube)
* Press the Scene dropdown menu and select "ADD NEW" → Full copy
* You can now go back to the Maze Surface section to make a new maze mesh
* Go back to level one via the Scene menu
* Select the End of level cube
* 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:WhenIwentintolevel2myplayernamechangedtoconeMat.001andIhadtochangeallmygoalsandthegiantrestartcubeassignments.)Answer:RenametheconeMat.001towhateveryouhadthematerialnamedforthefirstscene-thisfixeditforme.}$
$_{(noob:Inmylevel2myconehitsthecubeandnothinghappens!)Answer:MakesurethattheSensors,ControllersandActuatorstabsundertheLogicpanel(F4)areconnectedwithwires.}$
(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.
* Add a cube
* Scale it (in edit mode) to the dimensions 1,0.5,0.5
* Bevel it (WKEY) recursion 1, size 0.03 (press space to enter value manually)
* In logic panel, set it to Actor, Dynamic, Rigid body, Mass 0.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)
* 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.
$_{Ifyouhaveanyquestions,concerns,or'noobnotes',pleaseposttheminthediscussionpageforthistutorial,andnotinthetutorialitself.}$
| previous module: "Platformer: Creation and Controls" | • Table of Contents • Glossary | next module: "Platformer: Physics Fixes" |