How to size the camera in Unity (2D Project) - unity3d

I'm working on a 2D project in unity and I'm having trouble getting the camera to the exact size I need. I would like the camera to be centered around a level I've already created, so I know exactly how tall and wide it should be, and where it should be centered. Where I get lost is translating these values to the properties of the camera. Any advice would be appreciated.

You can put more details about your problem.
Unity's camera rect is resolution depend. So you cannot set width and height of camera size. For example if you have resolution 16:10, camera rect will have that ratio, you can only change scale of that real resolution.

Related

Unity on mobile cuts off sides (not canvas related), on different resolutions

I'm developing a 3d game on unity and using the 1080x1920 portrait resolution.
When I build and run the project on my phone, which has a 1080x2340 resolution in portrait, it cuts off part of the game (the sides).
red lines indicating where it cuts off
The camera doesn't follow, so the player can "go out of the screen".
How can I fix this, making sure my game looks the same on different resolutions?
(edited to correct resolution order)
Does your play area have a fixed size? If the in-game play area's aspect ratio isn't the same as the device running the game, you'll have to either cut off part of it or include blank space at the top/bottom or left/right. Unity's camera orthographic size dictates how tall the camera view is, but not how wide. The width is just screenAspectRatio * camera orthographic size.
To fix this:
float aspect = Screen.Width / Screen.Height;
myCamera.orthographicSize = Mathf.Max(playAreaHeight, playAreaWidth / aspect);
This will cause the camera's view to be either too wide or too tall rather than too small.

How do I make Pixel Perfect Camera scale like Normal Camera in Unity 2D

This is my scene
Right now if I use resolutions that aren't 16:9 the Pixel Perfect Camera zooms out quite a lot showing parts that shouldn't be shown.
How do I get it to scale like the Normal Camera which just cuts out parts outside the borders?
The Reference Resolution is currently set to 320x180, I tried changing the Reference Resolution to other resolutions (640x360, etc) but that changed nothing.
Tried Pixel Snapping on/off.
Cropping the X and Y does a good job at hiding the outside borders places but makes the screen too small for non 16:9 resolutions.

Unity Pixel per unit

i hope you can help me ... I chose a sprite background of size 2048x1365. My camera size is 6. The aspect ratio is 4:3. This is the math i did to claculate the PPU for that sprite:
I multiply my camera size by two to get my camera height and got 12.
Then I divided the height of the sprite by the height of the camera to get PPU and got 113.75 PPU.
Then I set the sprite PPU to 113.75 and the sprite size is still way smaller than the camera...
i hope you can help me .... thank you ...
I got this image from a Brackeys video. You want to divide the number of pixels of the image by the number of units it should take in the game. It sounds like you are on the right track with your math.
Here is an example I just made with the sprite's import settings.
And here is the sprite in the scene view.
I am able to successfully make the image cover the entire height of the camera.

Unity - 2D - Draw a sprite with it's "real" size

Is there a way to draw a 10x20px sprite always with 10x20px regardless of the resolution?
I don't mean to add it as a UI/canvas object, I want to be able to place it in the world and move it or move the camera.
thx for any help!
If using an Image component you can hit the Set Native Size button in the Inspector
Set the dimensions of the image box to the original pixel
size of the Texture.
In order to move it and place it in 3D it has to be a child of a Canvas with RenderMode = WorldSpace
Then also note the options of the Canvas especially the CanvasScaler -> Reference Pixels Per Unit value. It should be 1 in your case before hitting Set Native Size in the Image.
You can do that by setting the Camera ortographic size to the correct size. Try the 2D Pixel Perfect Camera package by Unity https://blogs.unity3d.com/2019/03/13/2d-pixel-perfect-how-to-set-up-your-unity-project-for-retro-8-bits-games/
The script in that package will automatically set your Camera ortographic size with provided parameters. If you want to manually set it, the formula is:
Camera ortographic size = vertical resolution / PPU / 2
Vertical resolution means the target vertical resolution that your sprite was designed for. For example, if your sprite is designed to look pixel perfect on 1600x900 screen, then the vertical resolution will be 900.
PPU means the pixel per unit, by default all imported sprites are set to 100.

Image size incorrect in Unity

I have a Unity 2D project with a fixed screen size of 800x450 pixels.
I have imported a background image that is also 800x450 pixels.
When placed on the stage, the image only takes up half of the screen.
The scale of the image is set to 1,1. The Z position is 0.
Why is the image displayed too small? How can I display the image at the correct resolution?
Does this mean that I have to design all my game assets at 2x the required size? Or that I somehow have to set the scale for all imported assets at 2? What is the recommended workflow?
EDIT
I have added a screenshot of the camera settings:
I would trying making your camera orthographic, and set the size of the camera (not the transform) to be half the height that you would like it to be (225)
Also if you are looking for pixel perfect game. here is a pretty good article from Unity about how to make that work and it explains some of the camera aspect ratios and scaling
http://blogs.unity3d.com/2015/06/19/pixel-perfect-2d/