Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Need help with BoxCollider2D bounding box position precision.

Discussion in '2D' started by hafizmrozlan, Jun 13, 2016.

  1. hafizmrozlan

    hafizmrozlan

    Joined:
    Jun 15, 2012
    Posts:
    117
    Hey guys,

    I'm making a 2D platforming game. Instead of using built-in physic2D, I opt to use custom collision. I'm still using BoxCollider2D as the bounding box though.

    To detect collision, I shoot a ray from the edge of bounding box + some secret space magic number.

    Upon debugging, I realise that the value returned from bounds.center and bounds.min or max always rounded up to one decimal point only. If my character center is 0.52 from the editor Transform, I only get 0.5 from the bounds.

    My character is one unit wide and tall. I don't know how much this will affect game play but I assume the collision detection won't be as precise as if I'm getting positional info in two decimal point. Furthermore my player is only one unit so the error can be as much as 5%.

    How do I fix this? Or is it totally negligible? Thanks!

    ps: I post the same question to Unity2D subreddit
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,539
    You say you're not using built-in 2D physics but you're using a BoxCollider2D which is. When you say you 'shoot a ray', do you mean you use 2D physics ray-casting? Obviously then you are using 2D physics. Confused.

    Why? It's a floating-point value, there's no rounding going on. Are you sure this isn't just the formatting going on when you output to the console or display as a field in the inspector. That's more likely what's going on. Also, know that the Bounds is an AABB; if the box is rotated in Z then it the Bounds and box sides won't line-up.
     
  3. hafizmrozlan

    hafizmrozlan

    Joined:
    Jun 15, 2012
    Posts:
    117
    Well, I don't use the rigidbody2D component, so I have to code all the gravity effect and alter the transform.position. sorry for not being specific.


    Hmmm, maybe you are right! I just print the variable I used to the console. Is there any way to do formatting on thing like Vector3 type variable when I put it in print("my variable type of Vector3")?
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,539
    If you don't add a Rigidbody2D then any Collider2D is static (attached to a hidden static body) and is not meant to be moved. You should add a Rigidbody2D set to be Kinematic if you intend to move it. You should also move it with Rigidbody2D.MovePosition and Rigidbody2D.MoveRotation.

    http://answers.unity3d.com/questions/173094/show-vector3-full-float-value.html