Search Unity

Why the hell does Unity's 2D interface work with X and Y?

Discussion in '2D' started by Bridgeman, Apr 13, 2014.

  1. Bridgeman

    Bridgeman

    Joined:
    Apr 13, 2014
    Posts:
    33
    As a 2D developer, I can't work with X and Y. 2D games don't use X and Y, they use pixels. Let's say I've made my pixel art background on a size of 640x480. Now how do I know what the appropiate X and Y values are in Unity to display it so that it's not stretched out, or squashed in?

    Being used to an engine like Game Maker, this sort of stuff just annoys me. What is X? What does it represent? How many pixels is "1 X" in Unity?

    Can't they just use pixel values for their 2D interface?
     
  2. Loius

    Loius

    Joined:
    Aug 16, 2012
    Posts:
    546
    Just set up your ortho camera's size so it matches your resolution, then your X and Y are pixel values.
     
  3. Bridgeman

    Bridgeman

    Joined:
    Apr 13, 2014
    Posts:
    33
    How do I do that?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's best not to think in terms of pixels anyway. Unity is a 3D engine, and uses arbitrary units for everything. Since screen sizes and resolutions are all different, trying to use a fixed pixel resolution for the game is usually not a good idea. Scaling is fine; for example FTL is a pixel-art game and still looks totally fine in stretched full-screen (scaled up from 1280x720).

    --Eric
     
  5. Bridgeman

    Bridgeman

    Joined:
    Apr 13, 2014
    Posts:
    33
    So in short Unity is useless as a 2D engine
     
  6. Thomas-Pasieka

    Thomas-Pasieka

    Joined:
    Sep 19, 2005
    Posts:
    2,174
    I am not sure if I should take you serious or not. There are plenty of great 2D games made with Unity (finished) out there. Your statement doesn't put a good light on you. May I suggest putting some more time and effort into whatever you're doing before coming around with headlines such as "Why the hell does Unity's 2D interface work with X and Y?" and phrases like "So in short Unity is useless as a 2D engine".

    In essence you can think of the 3 axis as in....

    Y represents up/down
    X represents left/right
    Z represents front/back

    Pretty simple.
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Check out some of the many 2D games made with Unity: https://unity3d.com/showcase/gallery Why you would even want to use pixels when you can use units instead of a bit of a mystery to me. But if you really need to, just do what Loius said.

    --Eric
     
  8. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    Why on Earth would I want to use a pixel scale in a 2D game when I'm developing for multiple platforms, any one of which uses multiple resolutions? The height of my game area is 10y for no better reason than because I felt like it; I could've picked any given number. The number of pixels that represents changes wildly, but the height of the game area does not, so I can use distances in this arbitrary scale without worrying about what my current resolution is.
     
  9. Bridgeman

    Bridgeman

    Joined:
    Apr 13, 2014
    Posts:
    33
    Yeah but X and Y are used for coordinates not for measurement. If I've made my sprites and backgrounds and objects all to scale in pixel art, there's no way I can get them in Unity with the same exact scales and sizes. I have to waste time screwing around with their "units" in order to figure something out that looks half decent.
     
  10. Thomas-Pasieka

    Thomas-Pasieka

    Joined:
    Sep 19, 2005
    Posts:
    2,174
    So you made your sprites to scale. What scale? How tall is one of your characters? Show me a sprite please. Don't use the "Transform" of your sprites to scale them. Use the "Pixel to Units" option in your import settings and make sure to use a constant number throughout. That will allow you to use the size you want and when you apply that to all your sprites it will retain your set "scale".
     
  11. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723

    Hate to break it to you but 2D games using X and Y has existed since 2D games began. It's you that's got to adjust.

    In unity's case you can just use Units, which is an even better measurement. We use 16 pixels / meter here.
     
  12. Thomas-Pasieka

    Thomas-Pasieka

    Joined:
    Sep 19, 2005
    Posts:
    2,174
    Here is an example. In the past I've created a rather popular 2D game called "The Other Brothers" along with Hippocoder as the developer (see above). All characters fit within a 64x64 sprite texture which was painted in Photoshop. According to that scale we scaled all other things. It's not exactly rocket science. Neither is the X,Y,Z system. Maybe it's time to sit down and really try to understand all these things first and get your scale set up right before you take it any further.

    You can use Pixel to Meters or make that 64x64 to be 2 meters.... You define it!

    https://dl.dropboxusercontent.com/u/449179/ThisAndThat/Scale.gif
     
  13. Bridgeman

    Bridgeman

    Joined:
    Apr 13, 2014
    Posts:
    33
    Fine, that may work for sprites. But now let's say I have a background I want to use for parallax scrolling, so I set it to a texture rather than a sprite, and then apply it to a quad. Let's say that background is 640x480. Now how do I know the right X and Y dimensions for the quad to fit those pixels? Sure, I can tile it, but it's still gonna look terrible unless I get the right scale.
     
  14. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Let's say it's not...there are many resolutions on many different devices. You can just set up the quad and position it so it fits in the camera view. That's pretty much all you have to do; no need to be concerned with pixel sizes and it will be scaled as needed. The only thing to be aware of is different aspect ratios, so you'd normally want to make sure that the background works for 4:3 as well as it does for 16:9 or even 2:1 (typically by having everything "important" inside the 4:3 area while the rest is "fluff").

    --Eric
     
  15. SiegfriedCroes

    SiegfriedCroes

    Joined:
    Oct 19, 2013
    Posts:
    569
    When making a 2D game in Unity, all graphics are Sprites, even a background should be imported as a Sprite. This way it will also be scaled right, as long as the Pixels to Units factor in the import settings is the same as the one for your other Sprites :)

    The approach is different and a bit more complicated when compared to Game Maker but this system does work better when targetting devices with different resolutions/aspect ratios ;)

    I'm currenly working on a 2D game (see signature) and it's 100% pixel perfect ;) So Unity is definitely capable of that, if you know what you're doing.
     
  16. Deleted User

    Deleted User

    Guest

    But this depend also on the case. If for example he want to put a background sprite that need scrolling, you would normally do by changing the uv offset of the mesh or the material offset properties. Now as long as I know with unity sprites you have no access on those properties so you would need to put another system on its place.

    I'm working on 2d game too and for my case I don' use unity 2d stuff at all, only collision and sprites for the ui, for the rest all normal meshes.

    To Bridgeman: is all a matter to do your own calculations to get a persistent aspect of your game on multiple resolutions. You sure have the pixel to units, but still you want to calculate your orthographic camera size to fit all res. Except for the pixels to units stuff there is no automatic scaling, is up to you.

    On my game I use a simple base, 1 Unit = 16 pixels, I scale all meshes depending on this without using unity 2d capabilities. From that too I calculate a camera size that will scale on multiple resolutions. From that I calculate the uv maps for each quad, but I'm using a tile based system, so its easier for me to deal with all that.
     
  17. GameDevGuy

    GameDevGuy

    Joined:
    Nov 4, 2012
    Posts:
    96
    There's also a technical purpose (constraint?) at play. The 2D system for Unity utilizes Box2D. Taken directly from the Box2D manual:

    From the Box2D FAQ:

    What units does Box2D use?
    Box2D is tuned for meters-kilograms-seconds (MKS). Your moving objects should be between 0.1 - 10 meters. Do not use pixels as units! You will get a jittery simulation.

    How do I convert pixels to meters?
    Suppose you have a sprite for a character that is 100x100 pixels. You decide to use a scaling factor that is 0.01. This will make the character physics box 1m x 1m. So go make a physics box that is 1x1. Now suppose the character starts out at pixel coordinate (345,679). So position the physics box at (3.45,6.79). Now simulate the physics world. Suppose the character physics box moves to (2.31,4.98), so move your character sprite to pixel coordinates (231,498). Now the only tricky part is choosing a scaling factor. This really depends on your game. You should try to get your moving objects in the range 0.1 - 10 meters, with 1 meter being the sweet spot.
     
    Last edited: Apr 14, 2014
  18. Loius

    Loius

    Joined:
    Aug 16, 2012
    Posts:
    546
    Set the ortho camera's "size" setting to the height you're targeting (so if you're targeting 640x480, set it to 480).

    As noted above, this will cause problems if you use the same camera for your physics'ed objects (since they'll be massive).