Need help with BoxCollider2D bounding box position precision.

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

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.

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”)?

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