Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Pixels per unit and gravity

Discussion in '2D' started by mattogodoy, Jul 21, 2016.

  1. mattogodoy

    mattogodoy

    Joined:
    Jul 21, 2016
    Posts:
    5
    Ok, I've been breaking my head for a while now about this subject, and I can't find any useful information. I've searched Google, Reddit, the Unity forums... nothing.

    I'm trying to create a platformer game. The style is pixel art and I have all the sprites in a single spritesheet, correctly sliced.
    Since it's pixel art, the sprites are very small (32 x 32). I understand the whole pixels per unit thing, but what I don't understand is how it relates with the gravity, or how gravity affects it's rigidBody2D.

    Let's say I have a character that is 32 pixels high. To round things up, let's say this character is very tall, and that in real life he's 2 meters tall. This leads to a pixels per unit value of 16. Am I right assuming this? If 32 pixels = 2 meters, then 16 pixels = 1 meter.

    Assuming that this is correct, now let's say that my character weighs 90 kilograms. I wan to keep values as near to real life as possible.

    I want the gravity to be real too, so I leave it as default (-9.8 units/second square).

    Summing things up, this is what I have:
    • Sprites size = 32 x 32 pixels
    • Pixels per unit = 16
    • Player mass = 90
    • Gravity 2D = -9.8
    So, the problem here (sorry that took me so long to get you into it) is that even when I think the numbers are ok, the gravity feels very weak. Almost kind of "lunar". The character falls very slowly.

    I can't understand why! I mean, if I change the pixels per unit value, what I'm changing is the scale of the assets, but I can't figure out how that affects to the physics! Regardless of the size of my player, if he has a mass of 90 and the gravity is -9.8, it should behave just like in the real world. What role do the pixels per unit play in the gravity of the game? How do I know which values to use?

    I haven't found much info, and what I would REALLY appreciate is some kind of formula. Something like: If you have x pixels per unit, you should set gravity to y.

    I know that I can increase the gravity until it "feels" right, but I'd like to understand why this happens. Also, increasing the gravity will lead to use grater forces to make him jump, which is not what I want since, as I already said, I'd like to keep things as near to real values as possible. Even the forces.

    Any help will be really appreciated and payed back with a hug if I see you someday somewhere.

    Sorry about my english and the long post.

    Thanks!
     
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    As it turns out, "real" gravity feels floaty in games. Jumping in real life is not a very responsive maneuver; especially when viewed at a distance, falling a few feet looks a lot less intense than it feels. This is especially true when talking about big objects.

    Consider this:



    The building appears to fall very slowly, because everything is accelerating at 9.8 meters per second per second, but from all the way over here, 9.8 meters doesn't look that big.

    A question for you: How high does your character jump? Is it higher than half an in-game meter or so? If that's the case, then it'll probably look slow. Real people typically can't jump upward more than a foot or so, so they're back on the ground in a matter of a second or two. If you create a character that jumps up more than double their height (which is typical for many games), it'll probably seem floaty, especially in 2D games where the character is viewed from a significant distance.

    In my game, I noticed that a gravity scale of 1.45 felt pretty good for my character's size. 1 was too floaty. Consider increasing the gravity on your player to make it less floaty. Even if your game's mechanics are physics based, you can often get away with blocks and such falling at a "real" acceleration even though your character falls faster.
     
    theANMATOR2b likes this.
  3. mattogodoy

    mattogodoy

    Joined:
    Jul 21, 2016
    Posts:
    5
    Thanks for your answer! Turns out you're right :)

    I've been doing some tests, and I was able to figure out something important. Look at this GIF:

    http://www.giphy.com/gifs/3oEjHXp1vHheFoddEA

    What you see are two boxes. The small one is set to 100 pixels per unit. The big one is set to 16 pixels per unit.

    As we can see, gravity applies exactly the same for both, but since the gravity is -9.8 UNITS/second square, the small box travels almost 10 times it's size per second, while the big box travels only once it's size per second. That's the reason everything looked so "lunar" to me. It's because the relative velocity is different for each box.

    The simple math I've come through is to increase the gravity to this value:

    100 / 16 * -9.8 = -61.25

    where 100 is the default pixels per unit, 16 is my sprite's pixels per unit and -9.8 is the default gravity.

    Setting the gravity to -61.25 makes my game feel a LOT better. This is the math I was after. It's not accurate regarding falling speeds, but it looks better.

    Also, I have to bear in mind that every force I apply will have to be multiplied by 16 (100/16) to keep it "real".

    I hope this will be valuable info for any Unity newbie like me in the future.
    Thanks!
     
    Hyblademin likes this.
  4. Martin_H

    Martin_H

    Joined:
    Jul 11, 2015
    Posts:
    4,436

    Wouldn't a "less hacky" solution to the problem be to change the pixelsize until the look of the physics match your expectations? Seems a lot simpler to me.
     
  5. mattogodoy

    mattogodoy

    Joined:
    Jul 21, 2016
    Posts:
    5
    With pixel size you mean the sprite images?
     
  6. Martin_H

    Martin_H

    Joined:
    Jul 11, 2015
    Posts:
    4,436
    Yes, the ratio how number of pixels translate to "unity units" (which afaik are meters). I'm sorry if I'm not explaining it well, I've done my first steps in the 2D side of Unity just recently.
     
  7. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    @Martin_H

    For the record, distances in Unity are unitless, but using meters as an analog is common.

    Scaling gravity up and scaling object size down are equivalent for OP's purposes. One increases the downward force, and one decreases the distance needed to travel for an object to cover n multiples of its length, which creates an illusion of increased force.

    If you zoom in on a LEGO block in real life (which appears to drop fast because of its small size), you can pretend everything is about the same height and make a tiny world of of blocks. But, if you redefine its height as 1 meter instead, then you also have to realize that gravity is now a bigger number, even though it means the same thing and objects still fall at the same rate.

    Both methods are equally hacky, but one of them requires only changing a minimum of one value, and the other requires changing potentially hundreds.
     
    Last edited: Jul 25, 2016
    theANMATOR2b likes this.