Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jakengelberg · Jul 07, 2019 at 06:09 AM · raycast3dvector3raycastingraycasthit

Boxcast vs Raycast Oddities

Weird issue I'm having :

I'm firing a 1 meter large boxcast DOWN towards the ground. It starts 1 meter off the ground and can at a maximum go down 1 meter. (so, it should have a hit distance of 1). The issue is that the boxcast says the hit distance is .9975, not 1.

A raycast starting at the bottom of the box going the same maximum distance of 1 meter registers a hit distance of 1. alt text

That makes make me think that the boxcast is starting .0025 meters too low, but the hit.point.y value always equates the raycast even when the distance differs.

In this example, the box/raycast is only .701 meters off the ground. The hit.point.y of both is equal, but the hit.distance differs. Any idea what is going on? Is it just an oversight by me? Thanks alt text

Boxcast is RED ▉ Raycast is GREEN |

alt textalt text

     void collision()
     {
         RaycastHit boxhit, rayhit;
         Physics.Raycast(new Vector3(0f, 1f, 0f), Vector3.down, out rayhit, 1f);
         Physics.BoxCast(new Vector3(0f, 1.5f, 0f), new Vector3(.5f, .5f, .5f), Vector3.down, out boxhit, Quaternion.identity, 1f);

         Debug.DrawRay(new Vector3(boxhit.point.x, 1f, boxhit.point.z), Vector3.down*boxhit.distance, Color.red);
         Debug.DrawRay(new Vector3(0f, 1f, 0f), Vector3.down*rayhit.distance, Color.green);
         Debug.Log("boxhit.point.y: " + boxhit.point.y + "    boxhit.distance: " + boxhit.distance);
         Debug.Log("rayhit.point.y: " + rayhit.point.y + "    rayhit.distance: " + rayhit.distance);
     }

UPDATE:

I even put in the Physics.Boxcast demo script (which was broken at first, using full size instead of halfExtents). And placing a boxes faces 1 meter apart from each other gives a hit distance of .9975. Is this a bug? So confused.

alt text

     void FixedUpdate()
     {
         //Test to see if there is a hit using a BoxCast
         //Calculate using the center of the GameObject's Collider(could also just use the GameObject's position), half the GameObject's size, the direction, the GameObject's rotation, and the maximum distance as variables.
         //Also fetch the hit data
         m_HitDetect = Physics.BoxCast(m_Collider.bounds.center, transform.localScale *1/2, transform.forward, out m_Hit, transform.rotation, 1f);
         if (m_HitDetect)
         {
             //Output the name of the Collider your Box hit
             Debug.Log("Hit : " + m_Hit.distance);
         }
     }


Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Jul 09, 2019 at 08:54 PM 0
Share

I think that's just how it works. It seems as if boxCast (only) distance is always short by 0.025, no matter what.

I really wanted to say there's a problem with your set-up. But I just tried my own test, and sure enough, got correct hit position (rounding error of 0.00000001), but distance is short by 0.0025. $$anonymous$$oved the cast into Update to let the target "settle down" but same thing. $$anonymous$$oved the box sideways, so i wasn't hitting the exact center -- same thing. Changed to a sphereCast -- the distance is now perfect. Only boxCasts have the problem.

avatar image NetEdge · Jul 01, 2021 at 09:46 PM 0
Share

One thing I just finally figured out is that (at least in 2019.4.22 LTS), a zero thickness collider being hit will give the right hit.distance and hit.collider, but the hit.point will always be identical to the starting point of the BoxCast. If you use Raycast with the exact same (applicable) parameters, it will work fine. Just thought I'd leave this here for posterity.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by MelvMay · Jul 06, 2019 at 07:18 AM

Both 2D (Box2D) and 3D (PhysX) use a "contact offset" which provides a buffer zone for collision-detection. This provides a region where a contact is considered touching and is used to provide a stable col-det simulation therefore they return where the collision would happen during normal simulation conditions. Look at positions during normal simulation; objects are not exactly aligned nor do they need to be apart from some sense of "precision".

Raycasts are not used during standard simulation and are there for miscellaneous use. They are calculate as the exact intersection.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jakengelberg · Jul 08, 2019 at 07:56 PM 0
Share

$$anonymous$$y Physics.defaultContactOffset is .01 (which divided by 4 is .0025 i guess). Does this still make sense? I can't relate these two numbers confidently. I'd like to have precision like a raycast but just in a box-like volume if possible.

avatar image Owen-Reynolds jakengelberg · Jul 09, 2019 at 01:31 AM 0
Share

I suppose you could test by changing that default to 0.2 (or anything relatively huge). See if it does anything. $$anonymous$$y feeling is *casts use exact math. The interpenetration a fudge factor for rigidbodies, to avoid twitching.

avatar image jakengelberg Owen-Reynolds · Jul 09, 2019 at 06:35 PM 0
Share

Yeah setting it to 20 doesn't do anything, but the exactness of the number makes it seem like some offset but I don't know what it is called.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

200 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

RaycastHit always returns 0 1 Answer

Problem With Raycasthit Angle 0 Answers

Raycast detects box collider, but not capsule collider 0 Answers

Can't find a clone object with RayCast? [Solved] 2 Answers

raycast in direction of movement key down 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges