1
\$\begingroup\$

If I have a GameObject, say at x = 0, y = 0, and I want to move it for a 40 pixel to x how can I make that?

In don't understand which is the unit of measurement in Unity.

If I make something like this where mov is 40 the sprite goes out of screen!

void Start()
{
    transform.Translate(new Vector3(mov, 0, 0));
}

it seems like the correct values must be very low, say 0.6, 1, 3, etc.

But why?

\$\endgroup\$
3
\$\begingroup\$

In Unity you don't use pixels as a distance measurement, you use units.

"How much is a unit?" well... it is whatever you want it to be. It's just a reference.

If you select your sprite in the assets window, in the inspector you will see it's properties. One of them is "Pixels per unit", which by default is 100.

If your sprite is 100x100 pixels, and you set it to 100 Pixels per unit, you sprite will be 1x1 units in your scene. If you want to use the pixels in your sprites as a size reference you have to use the same "Pixels per unit" in all of your sprites. There are several aproaches when it comes to deciding what Pixel per unit to use:

  • If you are using cell based sprites to generate terrain, you could use a Pixels per unit equal to the number of pixels of your cell's size. This way, each unit will be equal to 1 cell distance.

  • Set Pixels per unit to 1. This way 1 pixel will be the size of a unit. This is more intuitive when coding, but in the long run, I'd say its not really convenient. This would make game objects and collider HUGE. This might not look like a big issue, but it can actually impact the performance or behaviour of the physics engine for example. I have also experience interface problems with having such big objects: Having to zoom out some much, some gizmos stopped being displayed in the scene window, maybe because Unity though it was too far away to display.

  • Just leave it at 100. This is the default value, and it fits the rest of the default values: For example, gravity acceleration will look too slow if you set Pixels per unit at 1 and leave the default gravity force. If you are a begginer, and want to mess around with forces and masses, I'd suggest you take this approach, as this way if something doesn't work as you expected, it's provably because you did something wrong, and not because you didn't change some default value, or overlooked some other interactions.

Anyway you should take into account what Pixels per unit value you are using when measuring distances: units = pixels/pixel per unit

So, in your particular case, having the default Pixels per unit, to move the object for 60 pixel, the distance would be: units = pixels/pixel per unit => units = 60/100 => 0.6 units

\$\endgroup\$
  • 1
    \$\begingroup\$ Note that this answer is describing distance in terms of texture pixels, or texels. Depending on your camera size and output resolution, these may be bigger or smaller than pixels on the player's screen. Also, be very cautious about using a pixels per unit setting of 1: many game systems like physics are optimized assuming your dynamic content is a few units in size, give or take an order of magnitude. Making each unit a texel will often make your objects much bigger than these optimisations expect, so you may get degraded performance or funny-looking behaviour. \$\endgroup\$ – DMGregory Sep 16 '16 at 14:26
  • \$\begingroup\$ I knew there were some drawbacks on setting the ppu to one, but i was not sure which. Ill update it later, thanks \$\endgroup\$ – Leo Sep 16 '16 at 14:39
  • 1
    \$\begingroup\$ You can adjust the camera to work more easily with pixel measurements by switching it to orthographic and then adjusting the orthographicSize to be (pixelHeight / pixels per unit) / 2. \$\endgroup\$ – tyjkenn Sep 16 '16 at 17:30
0
\$\begingroup\$

The unit of measurement in Unity are "world space" units. Unity is still a 3d engine at hearth, and in 3d space, pixel coordinates are quite meaningless.

How world space coordinates translate to screen coordinates depends on how you set your camera. When you have a 2d project, then your camera is likely set to orthogonal projection. In that case you can use the size settings to control how many world space units are shown on the screen. With the camera on perspective projection it gets more complicated, because object scaling is affected by distance from the camera and field of view.

Keep in mind that changing your camera size also scales all your images. Use the "Pixels per unit " setting in the image import settings to control how large your images will be in the game. The question "How can I set up Unity so that 2d images are displayed in their original resolution?" goes into further details how to achieve a 1:1 scaling.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.