Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Jabbasmakt · Jul 04, 2020 at 12:25 PM · collisionraycastspheresurface normal

How do I find the normal of the closest surface (in a specific radius)?

Hello! So in my first attempt I used multiple Physics.Raycast in various directions and the one that had the shortest hit.distance, I used it's hit.normal to get the normal of that surface. This method could work but it requires a lot of raycasts in every direction to get a spherical distance check of all the objects in radius and it feels like this wouldn't be a very optimized solution.

So my next idea was to use Physics.SphereCast to cast a spherical ray, with a radius and 0 maxDistance. This method felt like it should've done the job since it is basically a spherical raycast in every direction, but the major issue with this is that the SphereCast will not detect any hits if it is already inside of an object. This means that only when an object is further away than the radius of the SphereCast will it detect a hit.

So now I'm kinda stuck on where to go next. I have looked at Physics.OverlapSphere but there doesn't seem to exist an easy way of getting the normal of the closest hit position using this method. Thank you for your time!


EDIT: After some testing it seems that OverlapSphere and ClosestPoint are very performance heavy, so in my case it's probably better to just use raycasts in multiple directions.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by kylecat6330 · Jul 04, 2020 at 03:12 PM

I'm not exactly sure what you are trying to do, so it is hard to say which is the best way to go about this, so I'll just go over doing it with OverlapSphere.

To use Physics.OverlapSphere here you'll need to use a for loop that'll check the distance of all colliders and stores the closest one.

 if (Vector3.Distance(collider[i].transform.position, SphereCenter) < Vector3.Distance(nearestObjPoint, SphereCenter) 
 {
      collider[i].transform.position = nearestObjPoint;
 }

After the for loop you can take the nearest point and calculate the direction to it using Vector3 direction = (SphereCenter - nearestObjPoint).normalized Then you can shoot a raycast at it to get the normal.

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
avatar image
0

Answer by Jabbasmakt · Jul 04, 2020 at 07:34 PM

@kylecat6330 Thanks for your help! I managed to get it working but I don't think it will work for my case. This because I need to check how close I am to surfaces, such as the terrain and map geometry. This means that when the sphere collides with something on the terrain it returns the centre position of the entire terrain, meaning that the ray will be angled towards the middle of the terrain, which might be very far away, and so the normal will not be the one that is closest position on the surface to the sphere.


This hopefully explains the issue I get: https://i.imgur.com/eAuEVHr.png

(I'm working in 3D, just a 2D representation)


EDIT: I used ClosestPoint() to find the closest point on the collider surface, and apparently ClosestPoint() doesn't work unless you have convex MeshColliders.

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
avatar image
0

Answer by kylecat6330 · Jul 04, 2020 at 08:10 PM

If you need the actual point of the collider which is closest to the collider then instead of using a for loop to check the nearest transform.position point use Collider.ClosestPoint, tho check for the closest point in each collider like this;

 if (Vector3.Distance(collider[i].ClosestPoint(SphereCenter), SphereCenter) < Vector3.Distance(nearestPoint, SphereCenter) 
 {
      collider[i].ClosestPoint(SphereCenter) = nearestPoint;
 }

Then after that you'd need to calculate the direction to the nearest closest point of all the colliders with Vector3 direction = (SphereCenter - nearestPoint).normalized; Once that is done you shoot a raycast at the point and you can get your normal from that.

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 Jabbasmakt · Jul 04, 2020 at 08:15 PM 0
Share

Thank you again. So I used ClosestPoint() and it works as intended, only issue is that ClosestPoint() doesn't work unless you have convex mesh colliders, so I gotta have to figure out how to turn my terrain meshes into that ..

avatar image kylecat6330 Jabbasmakt · Jul 04, 2020 at 09:12 PM 0
Share

You could try a ClosestPointOnBounds, but I've never used anything with bounds and I'm not completely sure how they work. That's kind of all I've got in terms of OverlapSphere. You could also try a for loop with an array of directions which you can pass through a raycast, so that you can use a single raycast to check multiple direction. However, I'm not sure how much more efficient that is than just using multiple raycast.

avatar image Jabbasmakt kylecat6330 · Jul 04, 2020 at 09:35 PM 0
Share

Doesn't seem to work with ClosestPointOnBounds, but still a huge thank you for your help. I think I will just create all my terrain collision meshes with a certain thickness to allow the ClosestPoint script to work.

EDIT: This will also fix an issue were some fast falling objects can fall through the map.

EDIT2: ClosestPoint still doesn't work even if you have a collision mesh with a thickness. It needs to be completely convex, so that makes it unusable in my case ...

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

235 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 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

Obstacle Avoidance 0 Answers

Collision between sambe objects with same Layer 1 Answer

Jumping with raycasts causing issues? 2 Answers

RayCast to all directions 4 Answers

Sphere falls through moving diagonal surface 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