How set 2D velocity in pixels?
I understand that velocity is measured in meters/second. I suppose this is a remnant of Unity's 3D roots where the world should operate independent of the scale of the viewport.
For 2D games this makes less sense, so I would like to calculate / set the speed of objects in pixels per second. How do I convert between the two?
Answer by Dave-Carlile · Sep 03, 2015 at 02:29 PM
When your sprites are imported there is a "Pixels Per Unit" property. That defines how many pixels go into a single Unity Unit. You should be able to use that value to map pixels to units.
For example, if your pixels per unit is 64, a 64x64 sprite would be 1x1 units. So if you want the sprite to move at 100 pixels per second, in units per second (i.e. meters per second) that would be 100/64 = 1.5625.
That said, I guess I've never seen anything that is specifying speeds in pixels. I personally would just make a normal sized sprite take up a single unit, pretend the unit is a meter, and set speeds and such using meters/second.
O$$anonymous$$, so if 1 unit is 100 pixels, and my velocity is Vector2(1,0) my object should move 100 pixels every second?
About specifying the speed in pixels, I guess it's not customary in Unity, but in any other 2D game engine (especially if you don't use physics) you'll always work with pixels. I see that complex 2D physics have a lot of factors to consider, and making the units abstract may actually be clearer.
Vector2(1, 0) should give you 100 pixels per second, yes. I think that's not necessarily going to be pixels on your monitor though... it depends on how your camera is set up, orthographic size, scaling and all that. If you have all that set up so it's pixel perfect though, that should be a true statement, but it's not something I've done.
Thanks! This works when I set the orthographic camera size to half the game height (600 pixels high = camera size 3)
Your answer
Follow this Question
Related Questions
2D ledge grabbing -1 Answers
(2D) Movement-Animation Problems 0 Answers
Placing a rectangle above ad 0 Answers
2D Dynamic Lights and Shadows 0 Answers