First Person Movement in the Blender Game Engine
Blender 3d tutorial
First Person Perspective
First person perspective is one of the many popular views in video games. To create a first person view, you simply treat the camera as the eyes of the main character. To move in the first person in a game in Blender 3D, we will go over various parts of the Blender Game Engine. This includes the basic components of the character and the usage of keyboard sensors to allowing for movement, turning, jumping, and crouching using only the Game Logic Bricks.
To start us off, I have created a basic floor and some crates: FirstPersonMovement-BlenderGameEngine001.zip. There is also a camera in the scene and a couple of light sources. Just to give us some reference so we know if our movement is working. All you really require is a plane to stand on and you should do fine, so if you don't use the blend file, that's okay too.
Even if you download my file, there are a couple setup steps. First, we want to change the Window Layout to Game Logic. This setting is directly to the right of the help menu at the very top of Blender. This will quickly open up the windows we will need.
Next, we need to change the render engine to Blender Game. You can find this setting at the very top of Blender to the right of the Window Layout and Scene selectors.
Preparing the Camera
Since we are doing first person movement in this tutorial, we need to place the camera at eye level. This means the most important position is the height on the Z-Axis. The exact height will depend on the scale of the scene you have created. For us, let's place our camera at these quardinates: (-8, -8, 3) and at this rotation: (90, 0, 0). These are also highlighted in the image above.
Now select the camera and go to the Physics panel of the Properties window. Since our camera will act as the character in our scene, change the Physics Type to Character. A handful of settings should appear.
To make sure we cannot run through things, check the Collision Bounds box and change the Bounds setting to Capsule. Compared to the other options, capsule seems to be the only one that performs without some strange quirks. Why that is, I do not know. The final setting we need to change is the Scale along the Y-Axis to 3. This is back in the Transform settings in the 3D view window.
Forward and Backward
Now that we have all of our objects set up, we can start moving. Make sure the camera is still selected. In the game logic window, click on the Add Sensor drop down and select Keyboad. Then rename this brick to W Key. Then click on the blank field to the right of the Key setting, and press W. This creates a sensor in the Blender Game engine that will activate when the W key is pressed.
Now, in the Actuators column, add a Motion actuator. Connect the dot next to our W Key sensor and connect it to the dot next to this new Motion actuator. You should see an And controller appear in the middle column when you do this.
In our motion actuator, Change the Motion Type setting to Character Motion and change the Loc setting's Z-Axis to -0.20. This will move the character forward.
Once you have done that, repeat these steps to create the backward movement. This time, use the S Key in the keyboard sensor and use a Z-Axis setting of 0.20 so the camera will move backwards.
Making the camera turn follows a very similar process a forwards and backwards. The main difference in this is case is using the rotation setting instead of the location setting.
So, like before, add a Keyboard sensor, rename it to A Key and set the Key setting to the A key. Then, add a Motion Actuator and connect our A Key sensor to it.
Just like before, we want to change the Motion Type to Character Motion. However, now we will change the Rot setting's Y-Axis to 4 degrees.
To turn to the right, we follow these same steps using the D Key in the Keyboard sensor and entering -4 in Y-Axis Rot setting in our Motion Actuator.
If you haven't already, you can test out your game by pressing the P button while hovering your mouse in the 3D view window. You should be able to move around the scene without going through our crates.
To go one step further, we are going to add jumping to our game. As usual, make sure the camera is selected and add a Keyboard sensor. This time, rename it Spacebar and set the Key setting to the Spacebar. Like before, add a Motion actuator and connect our logic bricks together.
In the new Motion actuator, change the Motion Type to Character Motion and turn on the Jump setting. Now if you test our your game by pressing P, you should be able to jump with the spacebar.
If you find you are jumping too high or not enough, you can change the Jump Force setting in the Physics panel when you have the camera selected. Turn it up to jump higher, down to jump lower.
Just for fun, lets add crouching to our set of movements. To do this, we will simply lower the height of our camera to make it feel like you are crouching.
Like always, add a Keyboard sensor. This time, we will use the Left Shift key in the keyboard sensor.
Add another Motion actuator and connect it to the sensor we just made. This time we will leave the Motion Type set to Simple Motion and change the Loc setting along the Y-Axis to -2.00.
Once done, test out your game by pressing P. You should have yourself a basic first person movement setup that includes jumping and crouching.
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!
- Character Physics Type, Blender 2.64 Release Notes
- Character Physics Type, Blender 2.66 Release Notes
- Keyboard Sensor, Blender Docs