Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Change Physics2D scale?

Discussion in '2D' started by any_user, Sep 16, 2014.

  1. any_user

    any_user

    Joined:
    Oct 19, 2008
    Posts:
    374
    I'm working on a 2D game with physics and I often run into situations where the collider is too small for the physics engine.

    That's the error message in inspector:
    This happens when I generate a poly collider through sprites but also when I create one manually. My scene height is two world units (= screen height) and the physics behave normally, except for a few things related to this. For example there is a noticeable padding around all colliders, and I found no setting to change that. I always thought it's good to have objects in a size of around 1 unit, but it doesn't seem to apply for 2d physics.

    Is there a way to change the scale these collider "optimizations" so they don't interfere with my game?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    It's ideal if the object is 1 unit, correct. If it's that big you won't get errors about collision shapes being too small. If your entire scene is only 2 units high then it seems the objects would have to be much smaller than 1 unit. The only way to fix the problem is to scale the objects up, because the vertices are physically too close and there's nothing the engine can do about that.

    --Eric
     
  3. any_user

    any_user

    Joined:
    Oct 19, 2008
    Posts:
    374
    Yeah I guess that's the problem, the biggest objects are 2-3 units the smallest 0.02. They're not in the same scene, so I could rescale some scenes, but that would be quite complicated. When working with box2d in flash some years back, I had a scale value that says how many units in the physics engine are mapped to 1 pixel. In unity that would be a physics units to world unit conversion, is there a way to do that?

    If that's not possible, what is the ideal range for objects? Is it better to go up from 1 (eg. boxes with side length 5-10) or to go down to 0.1-0.2? What's the upper size limit for stable physics calculations?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Physics units are world units and this can't be changed. Unity has a pixel-to-units setting for importing sprites, which is 100 by default, so a 100 pixel sprite becomes 1 unit. Box2D (which Unity uses for 2D physics) prefers moving objects in the range of 0.1 - 10.0, with 1.0 being ideal.

    --Eric
     
    Pavulon likes this.
  5. any_user

    any_user

    Joined:
    Oct 19, 2008
    Posts:
    374
    Ok, thanks for the info!
     
  6. cowlinator

    cowlinator

    Joined:
    Mar 15, 2012
    Posts:
    69
    I was having this issue, and it wasn't being resolved by changing the scale. Turns out if you create a SpriteRenderer at runtime, you have to attach the collider2D only after you assign a sprite to it.
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,414
    That's simply not true; 2D colliders don't interact with sprites. Certainly, as a convenience, if you add a 2D collider onto a GameObject that already has a SpriteRenderer then it'll set the size of the collider to match any assigned sprite but that's it. If there's no SpriteRenderer then it'll assume its default size.
     
  8. cowlinator

    cowlinator

    Joined:
    Mar 15, 2012
    Posts:
    69
    How do you change the size of the collider at runtime?
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,414
    The BoxCollider2D has a size property, the CircleCollider2D has a radius property and the edge and polygon colliders are just a set of vertices so can't be directly sized but all will be sized by the TransformComponent scale X and Y.
     
  10. Myrddins_Owl

    Myrddins_Owl

    Joined:
    Apr 24, 2014
    Posts:
    4
    I am running into this problem since I am upgrading from Unity 4.3.4 (and the polygons were created without a problem at the time) to Unity 5. When I first started this project last May, I almost surely selected the wrong import settings for my sprites, but now that I am close to completing the project, I have vertices that are being ignored throughout the entire project on polygon colliders, and box colliders that are too small.

    Yes increasing the scale of the object will make the colliders appear, but as that will require going through all of my scenes and modifying everywhere that I change the scale throughout the code, I am trying to find another way to get things to work.

    Is it possible to modify the minimum size within Box2D within Unity or is there a variable somewhere that can be adjusted to change this globally?
     
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,414
    Ignoring those vertices didn't change really, it's just that we report/show them now as there were lots of bug reports where users would say that edges were missing at small scales. A while back, Box2D had 'undefined' results; actually there were debug-mode asserts within Box2D) with such edges then later there was an explicit check added to it which merged close proximity vertices.

    It's a limit but it's a limit based on a very tiny distance between vertices. It cannot be changed because it's based upon a fundamental constant that's used throughout Box2D. Mostly it can occur when you create very small fan-like triangles when a polygon path that is curved is used.
     
    Last edited: Apr 17, 2015
  12. Chaoseiro

    Chaoseiro

    Joined:
    Aug 28, 2013
    Posts:
    40
    So... I've also upgraded from 4.3.4 to 5, my game uses 128px per unit and I have a "bullet capsule" sprite that is 4x2px. It worked well in 4.3, but in order for it to work in 5, I had to scale it at least 4x...
    If I want to use this 4x2 sprite, I'll really have to scale up my whole game (use 32px/unit)???
    Even using 100px/unit won't work, and a BoxCollider2D with size 0.04x0.02 will throw the "failed verification" error...
    Can't this constant be changed in the Physics2DSettings?
     
  13. HassanS

    HassanS

    Joined:
    May 31, 2013
    Posts:
    4
    Hi I have the exact same problem as you now when I upgraded to unity 5. Did you find any solution to this?
     
  14. Chaoseiro

    Chaoseiro

    Joined:
    Aug 28, 2013
    Posts:
    40
    Unfortunately my solution was to scale up the whole game 4x... (from 128 px/unit to 32 px/unit)
    I had to make adjustments to some calculations, my camera script and variables that dealt with forces and speeds.
     
  15. shaneparsons

    shaneparsons

    Joined:
    May 5, 2015
    Posts:
    44
    Where / how do you change the scale of the game?