# AttachTo

### Introduction

The Camera class inherits from the [PositionedObject](https://github.com/flatredball/FlatRedBallDocs/blob/main/frb/docs/index.php) class, so it can use the AttachTo function. For general information on AttachTo, see the [PositionedObject.AttachTo](https://github.com/flatredball/FlatRedBallDocs/blob/main/frb/docs/index.php) page.

### Attaching a Camera to another object

The simplest way to have the Camera follow an object is to attach the Camera to the object. However, if you do this, you will need to make sure to have the Camera's RelativeZ be a positive value so that the Camera isn't at the same Z value as the object it is attached to. For example, the following code attaches a Camera to a PlayerInstance - which is assumed to be a PositionedObject:

```
SpriteManager.Camera.AttachTo(PlayerInstance, true);
SpriteManager.Camera.RelativeX = 0;
SpriteManager.Camera.RelativeY = 0;
SpriteManager.Camera.RelativeZ = 40;
```

### AttachTo vs. Manual Camera Following

Attaching a Camera to another PositionedObject (like an Entity instance) is the easiest way to have the Camera follow on object; however, it is rather limited in its movement. Most modern, professional games do not attach the Camera to the player. The reason is this results in an unnatural form of movement. Rather, most modern games will do the following:

* Perform smoothing in the movement - often the Camera may lag behind player actions
* Looking ahead towards where the player is facing
* Adjusting to prevent the "end of the world" from being seen
* Scripted movement such as zooming out or moving on game events like when entering a new environment or when a boss appears

Therefore, attaching the Camera to an object is a good initial following implementation, but you should consider replacing this with a more advanced implementation as your game moves further along.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.flatredball.com/flatredball/api/flatredball/camera/attachto.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
