Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Whipexx · May 03, 2016 at 02:17 PM · physicsraycastingmeshcollider

Physics.Boxcast not working with mesh collider

Hi

I've been trying to make Physics.Boxcast work and spent a few hours fiddling with it without much success, until I discovered that it apparently ignores mesh colliders. I've tried replacing my mesh with a box collider and then everything starts working like a charm; but I really need my collider to have the same shape as my mesh, so a box collider will just not work.

Does anyone know if this is a known issue? May I be doing something wrong?

My code does the following: It gets two items, a terrain like(but not a unity terrain) mesh (target) and a cube clipping with that mesh in y axis (origin). Then it trows a boxcast in the containing box of the cube to detect the lowest point of the mesh, still in the cube's bouding area; I'm getting the lowest point of the mesh located below the cube.

 public static float LocateLowestCollidingSection(GameObject origin, GameObject target){
         
 
         Mesh originMesh = origin.GetComponent<MeshFilter>().mesh;
         RaycastHit hit;
         float searchMargin = 50f;
 
         if (Physics.BoxCast (new Vector3 (origin.transform.position.x, origin.transform.position.y - searchMargin, //- originMesh.bounds.extents.y/2f, 
             origin.transform.position.z),
             new Vector3 (originMesh.bounds.extents.x, 0.1f, originMesh.bounds.extents.z), 
             Vector3.up, out hit, Quaternion.identity, searchMargin + originMesh.bounds.extents.y, ~target.layer)){
 
             return hit.point.y;
         } else {
             
             Debug.LogError ("And thus, only ethernal void remains below the target.");
             return Mathf.Infinity;
         }
     }
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 FortisVenaliter · May 03, 2016 at 08:05 PM 0
Share

Hmmm... not sure entirely, but here's two things to know that may lead you to an answer:

First, is that mesh colliders will not collide with other mesh colliders. So, they are special in some ways, but I don't see anything in the docs that would indicate BoxCast wouldn't work.

Second, the raycast/boxcast docs say that if the origin is within the collider's bounds, it won't trigger a collision (e.g. a raycast starting in the middle of a sphere and moving out won't hit the outside of the sphere). So maybe you need to move back your origin point to ensure it's outside of the mesh collider's bounds?

avatar image Whipexx FortisVenaliter · May 04, 2016 at 08:24 AM 0
Share

I've tried moving the cast away from the mesh, in fact in my example I use a huge margin of 50f, while both my target and origin are both (0,0,0). The fact that boxcast will not work if already inside the object makes me think that the fact that the mesh collider is not convex might have something to do with that.

But as far as I remember raycasts worked fine when I tried them... I'll try a couple of things and comment again with the conclusions, still think it might be an issue with boxcast.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Whipexx · May 04, 2016 at 01:24 PM

Welp, turns out it was indeed my fault.

Neither boxCast nor rayCast will hit a mesh on its backside, and since my mesh needs to be upwards (because I need a player to be able to walk on it) what needs to be done is either:

Change the way of obtaining that point: Throwing raycasts from the top may be a good approximation, although it might not be precise or be too resource consuming if the mesh is huge and has detail. I've not tested this but it should work.

Rotate our mesh before we use the boxcast method and rotate it again after measuring so that the player does not notice the change. Does not sound efficient to me either, but I've tried doing this on update and can barely notice the difference (and I will only do this once, not on Update when it's implemented).

For anyone in the same situation as me, I used carmine's code to invert my mesh: http://answers.unity3d.com/questions/213427/sphere-collider-invert.html

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Find closest point on all objects in a layermask 1 Answer

Check whether objects are within a sphere of influence 1 Answer

Why does RaycastCommand doesn't hit back faces even when `Physics.queriesHitBackfaces` is set to true? 0 Answers

Physics Raycast gives hit in wrong place 0 Answers

Raycast does not work with touch input but works well with mouse input why? 0 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