> For the complete documentation index, see [llms.txt](https://docs.flatredball.com/flatredball/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flatredball.com/flatredball/api/flatredball/camera/pixelsperunitat.md).

# PixelsPerUnitAt

### Introduction

The PixelsPerUnit method is a method that can be used to convert between world units and screen pixels. The PixesPerUnit method can be used if pixel coordinates are desired when the 3D camera is being used.

### Method Signature

```
public float PixelsPerUnitAt(float absoluteZ)
public float PixelsPerUnitAt(ref Vector3 absolutePosition)
public float PixelsPerUnitAt(ref Vector3 absolutePosition, float fieldOfView, bool orthogonal, float orthogonalHeight)
```

### Common usage

The most common usage of PixelsPerUnit is to convert from pixels to units. In other words, the most common usage is to obtain "world units per pixel". Once the number of world units is obtained per pixel, multiplying that value can be multiplied by the desired number of pixels. To convert "pixels per unit" to "units per pixels", we simply need to take the reciprocal. If you're not familiar with this math term, taking the reciprocal of a value is the same as dividing one by the value. The code for this is as follows:

```
float pixelsPerUnit = Camera.Main.PixelsPerUnitAt(0);
float unitsPerPixel = 1 / pixelsPerUnit;
int desiredPixels = 64;
float worldUnits = unitsPerPixel * desiredPixels;
```

### Code Example - Setting Sprite Width and Height

The following code example creates a [Sprite](https://github.com/flatredball/FlatRedBallDocs/tree/main/frb/docs/index.php) and scales it to the size of the entire screen. The default resolution is 800 X 600, so scaling the [Sprite](https://github.com/flatredball/FlatRedBallDocs/tree/main/frb/docs/index.php) to this size will make the [Sprite](https://github.com/flatredball/FlatRedBallDocs/tree/main/frb/docs/index.php) fill up the entire screen. Add the following to Initialize after initializing FlatRedBall:

```
// Our default resolution is 800 X 600
int desiredSpritePixelWidth = 800;
int desiredSpritePixelHeight = 600;

// remember, Scale is half of width
sprite.Width= PixelsToUnits(
    desiredSpritePixelWidth,
    sprite.Z);
sprite.Height = PixelsToUnits(
    desiredSpritePixelHeight,
    sprite.Z);
```

Add the PixelsToUnits method at class scope:

```
float PixelsToUnits(int numberOfPixels, float absoluteZ)
{
    return numberOfPixels / 
        Camera.Main.PixelsPerUnitAt(absoluteZ);
}
```

![PixelsPerUnit.png](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-a3b41ca544a2e2e0e1697c4a758dff30889a305e%2Fmigrated_media-PixelsPerUnit.png?alt=media)

### Code Example - Spacing Objects on Screen

This example creates a row of circles, each touching end-to-end, 32 pixels apart on a 3D camera. It uses PixelsPerUnitAt to size and space the circles. The following code can be placed in a screen's CustomInitialize method:

```lang:c#
void CustomInitialize()
{
    // This could also be set in Glue by unchecking Is 2D
    Camera.Main.Orthogonal = false;

    float leftEdge = Camera.Main.AbsoluteLeftXEdgeAt(0);
    float unitsPerPixel = 1 / Camera.Main.PixelsPerUnitAt(0);

    float firstCircleX = leftEdge + unitsPerPixel * 16;
    float circleRadius = unitsPerPixel * 16;
    float circleSpacing = unitsPerPixel * 32;

    float currentX = firstCircleX;

    for(int i = 0; i < 24; i++)
    {
        Circle circle = ShapeManager.AddCircle();
        circle.Radius = circleRadius;
        circle.X = currentX;
        currentX += circleSpacing;
    }
}
```

![](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-3f300eb044bb0f147df1b7d06e86143f103e0bf5%2F2017-02-img_589de828605c9.png?alt=media)

### PixelsPerUnitAt and FieldOfView

If you are using a 3D Camera, then the FieldOfView impacts the PixelsPerUnitAt method. PixelsPerUnitAt measures the number of pixels per world unit. In other words, the formula is: PixelsPerUnit = NumberOfPixels / NumberOfWorldUnits Therefore, increasing NumberOfWorldUnits will decrease the PixelsPerUnitAt. Increasing the FieldOfView will increase the number of visible world units, but will keep the NumberOfPixels constant. Therefore, a larger FieldOfView will decrease PixelsPerUnitAt

### PixelsPerUnitAt and resolution

The resolution of the game impacts the PixelsPerUnitAt value. Increasing the resolution increases the NumberOfPixels in the formula above. Therefore, if your game runs at higher resolution, then the PixelsPerUnitAt will be larger. Keep in mind that by default the FlatRedBall Camera uses the same FieldOfView. This means that if your game increases its resolution, the PixelsPerUnitAt will increase. To be more specific, the PixelsPerUnitAt uses the resolution height. Therefore, changing the width of the application will not impact the PixelsPerUnitAt, but changing the height will.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.flatredball.com/flatredball/api/flatredball/camera/pixelsperunitat.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
