Converting a pixel coordinate to a unity unit coordinate?
Hi, I am making a 2D space shooter type game. I have multiple ships and on some of the ships I have multiple guns. I know the pixels coordinates in the image of each gun. Right now I am trying to make the lasers spawn at the local position + the position of the pixel in the image. For example I have a gun at the position 24, 1 in the image. I am trying to spawn it at: (transform.localPosition.x + 24, transform.localPosition.y + 1 )
This doesn't work because unity uses units not pixels. Is there any way I can convert pixels to units?
Answer by Socapex · Dec 28, 2016 at 11:22 PM
That is actually a good question. To get the ratio, use sprite.pixelsPerUnit. Then add your number / ppu. For example, 24 / ppu
.
Another solution that I personally use, if you are working in pixel art, is to import all your sprites a 1 PPU. That way 1 unit = 1 pixel. It makes it easier to think about your game and distances.
Finally, depending on how much your art changes, you could also use a gameObject that you position on the prefab itself. This decouples the design from the art and might make things easier in the long run.