2

I'm using Physics2D.BoxCast in Unity(2019.4.19f1).

I have a collider2D at position==(0,0) with scale==(1,1)
Then I call a Physics2D.BoxCast:

var hit = Physics2D.BoxCast(new Vector2(0, 2), new Vector2(1, 1), 0, new Vector2(0, -1));

It is a Box Ray Cast like the following pic:
enter image description here

I have a hit returned.
But hit.distance == 0.9950001, which is supposed to be 1.
I guess there may be some inaccuracy in Physics2D.BoxCast. But I have no idea how much the inaccuracy can be.

Then I do some searching and find "Default Contact Offset" may be related to my problem.
I change "Project Setting → Physics2D → Default Contact Offset" to 0.001(default is 0.01).
Run the code again. But now I get hit.collider == null.

Now I'm more confused about Physics2D.BoxCast.
It may be helpful to learn something about how Physics2D.BoxCast work. But I do not know where to find them.


Added 2021.3.1
I did several experiments about "DefaultContactOffset".(link)
The experiment results show that:

  1. If the DefaultContactOffset is about 0.002~0.01. Most of the time, the error in hit.distance is about 0.005.
  2. If the DefaultContactOffset is about 0.02~0.1. The error in hit.distance is larger.
AdmiralOrange
  • 157
  • 1
  • 14
  • This question is about [tag:c#], not [tag:unityscript]. – Ruzihm Apr 06 '21 at 21:09
  • @Ruzihm Can you explain more about this? For me, I think it is about Unity, it is about Unity's Physics2D.BoxCast function. – AdmiralOrange Apr 07 '21 at 08:22
  • [tag:unityscript] is a **deprecated** javascript-like language used in versions prior to 2018.2. This question is about [tag:c#] in [tag:unity3d] and has nothing to do with unityscript. For an example of a question that does involve unityscript, see [here](https://stackoverflow.com/q/51638758/1092820). – Ruzihm Apr 07 '21 at 15:07
  • @Ruzihm Got it. Thank you for you help. I'm quite new to Unity and didn't know that. – AdmiralOrange Apr 08 '21 at 09:28

1 Answers1

1

I actually don't know Physics2D.BoxCast but from what i understand it just moves a box to your target at a "speed". Now I done similar calculations in opengl and any collision like this using a speed over time will result this behavior. You can't expect to get a result %100 accurate. imagine every frame you move the red box 5 points instead of a small scale. You red box would just jump over the white box and you would not get a hit result..

Now internet says " Reducing Default Contact Offset can mean that collisions don't have enough of a buffer to work correctly. " so i guess Physics2D.BoxCast just check if 2 objects is going to collide and not if they are colliding which would explain why you get a null hit when you make the ofset so small.

Edit:This was a comment but was too long.

Xentios
  • 84
  • 1
  • 10
  • Thank you. I do several experiments about "DefaultContactOffset".([link](https://www.notion.so/Physics2D-BoxCast-4effcbffdc624e22be0dc11f33cef5dc) ), and it looks "using a speed over time". – AdmiralOrange Mar 01 '21 at 04:12
  • The experiment results show that: ____1.If the DefaultContactOffset is about 0.002~0.01. Most of the time, the error in hit.distance is about 0.005. ---> (this looks like "using a speed over time") ____2.If the DefaultContactOffset is about 0.02~0.1. The error in hit.distance is larger. – AdmiralOrange Mar 01 '21 at 04:13
  • My thoughts about how BoxCast works: ____1.If BoxCast works like "using a speed over time". Then, when the collider is very far away, it can be very inefficient. ____2.Then I thought BoxCast may work like "trying distance 1,2,4,8,16...". But, how to avoid missing collider? ____3.Then I thought I am unable to know how it works. It may be a combinatin of the aboves and several other algorithms I don't know. – AdmiralOrange Mar 01 '21 at 04:17
  • As for hit==null when DefaultContactOffset == 0.01, I still have doubts. The algorithm should be ralated to DefaultContactOffset. DefaultContactOffset is about how colliding should be checked. "Physics2D.BoxCast just check if 2 objects is going to collide and not if they are colliding" can not explain all for me. – AdmiralOrange Mar 01 '21 at 04:18
  • Where did you find " Reducing Default Contact Offset can mean that collisions don't have enough of a buffer to work correctly. ". I didn't see it in Unity Manual. – AdmiralOrange Mar 01 '21 at 06:13
  • This is why it is "internet says" not unity. I really don't remember where was it from. I suggest use this with normal values until a pro comes and answers all your questions. – Xentios Mar 01 '21 at 06:20