# How To: Make a Third-Person Camera

XNA Game Studio 3.0
Demonstrates how to create a third-person camera.

The example controls the camera using the method shown in How To: Rotate and Move a Camera, with an additional offset for the camera so it is looking down at the game avatar. Turning the avatar causes the camera to rotate around the avatar's position. The current camera type is displayed in the upper-left corner of the screen and updated whenever the TAB or left shoulder button is pressed.

The example assumes a model has been added to the project as described in How To: Render a Model.

# The Complete Sample

The code in the topic shows you the technique. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.

# Making a Third-Person Camera

### To make a third-person camera

1. Decide on a camera offset vector from the avatar.

An offset of the form (0, N, −N) will put the camera above and behind the avatar at a 45-degree angle.

```Vector3 thirdPersonReference = new Vector3(0, 200, -200);
```
2. Track the position and rotation of the avatar.

Next, you need to create a rotation Matrix for the direction that the avatar is facing.

3. Create the rotation Matrix with CreateRotationY, using the avatar's current rotation as its parameter.

```Matrix rotationMatrix = Matrix.CreateRotationY(avatarYaw);
```
4. Transform a copy of the camera offset vector by using the Transform method and the rotation Matrix.

```// Create a vector pointing the direction the camera is facing.
Vector3 transformedReference = Vector3.Transform(thirdPersonReference, rotationMatrix);
```
5. Calculate the camera's position as the avatar's position, and calculate the transformed camera offset.

```// Calculate the position the camera is looking from.
Vector3 cameraPosition = transformedReference + avatarPosition;
```
6. Create a view Matrix with the CreateLookAt method.

The avatar's current position will be the cameraTarget parameter.

```view = Matrix.CreateLookAt(cameraPosition, avatarPosition, new Vector3(0.0f, 1.0f, 0.0f));
```
7. Create a new projection Matrix with CreatePerspectiveFieldOfView.

The projection Matrix controls how camera coordinate values are transformed to screen coordinates.

```Viewport viewport = graphics.GraphicsDevice.Viewport;
float aspectRatio = (float)viewport.Width / (float)viewport.Height;

proj = Matrix.CreatePerspectiveFieldOfView(viewAngle, aspectRatio, nearClip, farClip);
```
8. Loop through each model in the world drawing it as described in How To: Render a Model using the projection matrix and view matrix created above.

For the world matrix, use Matrix.CreateTranslation and the object's current position in the world.

```void DrawBoxes()
{
for (int z = 0; z < 9; z++)
{
for (int x = 0; x < 9; x++)
{
DrawModel(box, Matrix.CreateTranslation(x * 60, 0, z * 60), boxTexture);
}
}
}

/// <summary>
/// Draws the 3D specified model.
/// </summary>
/// <param name="model">The 3D model being drawn.</param>
/// <param name="world">Transformation matrix for world coords.</param>
/// <param name="texture">Texture used for the drawn 3D model.</param>
void DrawModel(Model model, Matrix world, Texture2D texture)
{
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect be in mesh.Effects)
{
be.Projection = proj;
be.View = view;
be.World = world;
be.Texture = texture;
be.TextureEnabled = true;
}
mesh.Draw();
}
}
```