Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 mat · Feb 18, 2011 at 09:49 AM · meshpointcontain

Finding if point is contained within irregular mesh

Is there something like bounds.Contain() that instead of returning true if a point is contained within a bounding box, returns true if the point is within the volume defined by the mesh collider? (So it would work if the mesh was a doughnut shape)

Comment
Add comment · Show 1
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 intrepidis · Nov 16, 2014 at 05:14 PM 0
Share

Check out this answer to a similar question: http://answers.unity3d.com/questions/611947/am-i-inside-a-volume-without-colliders.html#answer-832969

3 Replies

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

Answer by Eric5h5 · Feb 18, 2011 at 10:27 AM

There's nothing built-in, but you can make whatever functions you want. See here for a function that checks if a point is inside an arbitrary 2D shape.


edit (copy from wiki)

 // Poly.cs
 using UnityEngine;
  
 public static class Poly
 {
     public static bool ContainsPoint(Vector2[] polyPoints, Vector2 p)
     {
         var j = polyPoints.Length - 1;
         var inside = false;
         for (int i = 0; i < polyPoints.Length; j = i++)
         {
             var pi = polyPoints[i];
             var pj = polyPoints[j];
             if (((pi.y <= p.y && p.y < pj.y) || (pj.y <= p.y && p.y < pi.y)) &&
                 (p.x < (pj.x - pi.x) * (p.y - pi.y) / (pj.y - pi.y) + pi.x))
                 inside = !inside;
         }
         return inside;
     }
 }
Comment
Add comment · Show 6 · 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 Gillissie · Apr 16, 2011 at 02:39 AM 0
Share

Eric, how in the world did you come up with that function? Totally sweet!

avatar image Eric5h5 · Apr 16, 2011 at 03:09 AM 0
Share

@Gillissie: Google. I'm not that smart. ;)

avatar image Nisal_Dilshan · Jun 23, 2019 at 08:27 AM 1
Share

broken link!

avatar image Bunny83 Nisal_Dilshan · Jun 23, 2019 at 10:16 AM 0
Share

fixed the link, thanks

avatar image sezerBunyamin · May 16 at 11:31 AM 0
Share

Can you fix the link?

avatar image Bunny83 sezerBunyamin · May 16 at 01:45 PM 1
Share

The Unity wiki does no longer exist. However we have the wayback machine (archive.org). So I fixed the link, again ^^.

avatar image
4

Answer by IsGreen · Feb 05, 2014 at 08:20 PM

Script Action Video: http://www.youtube.com/watch?v=O2xcTvWBRiU#t=0

 using UnityEngine;
 using System.Collections;
 
 public class csObject : MonoBehaviour {
 
     public MeshCollider meshCollider;
     public bool In;
     public bool concaveHull;
     public float distance=100f;
 
     Ray right,left,up,down,forward,back,tempRay;
     bool r,l,u,d,f,b;
 
     RaycastHit rightHit   = new RaycastHit();
     RaycastHit leftHit    = new RaycastHit();
     RaycastHit upHit      = new RaycastHit();
     RaycastHit downHit    = new RaycastHit();
     RaycastHit forwardHit = new RaycastHit();
     RaycastHit backHit    = new RaycastHit();
     RaycastHit tempHit    = new RaycastHit();
 
     void Start(){
 
         right   = new Ray(Vector3.zero , -Vector3.right);
         left    = new Ray(Vector3.zero , -Vector3.left);
         up      = new Ray(Vector3.zero , -Vector3.up);
         down    = new Ray(Vector3.zero , -Vector3.down);
         forward = new Ray(Vector3.zero , -Vector3.forward);
         back    = new Ray(Vector3.zero , -Vector3.back);
         tempRay = new Ray();
 
     }
 
     bool ConcaveHull(Ray ray, RaycastHit hit){
 
 
         tempRay.origin = transform.position;
         tempRay.direction = -ray.direction;
         float customDistance = distance-hit.distance;
         int lastPoint = hit.triangleIndex;
 
         while(meshCollider.Raycast(tempRay, out tempHit, customDistance)){
 
             if(tempHit.triangleIndex == lastPoint) break;
             lastPoint = tempHit.triangleIndex;
             customDistance = tempHit.distance;
             ray.origin = -ray.direction * customDistance + transform.position;
 
             if(!meshCollider.Raycast(ray, out tempHit, customDistance)) {
 
                 concaveHull = true;
                 return true;
 
             }
 
             if(tempHit.triangleIndex == lastPoint) break;
             lastPoint = tempHit.triangleIndex;
             customDistance -= tempHit.distance;
 
         }
 
         return false;
 
     }
 
     // Update is called once per frame
     void FixedUpdate () {
     
         right.origin   = -right.direction   * distance + transform.position;
         left.origin    = -left.direction    * distance + transform.position;
         up.origin      = -up.direction      * distance + transform.position;
         down.origin    = -down.direction    * distance + transform.position;
         forward.origin = -forward.direction * distance + transform.position;
         back.origin    = -back.direction    * distance + transform.position;
 
         r = meshCollider.Raycast(right   , out rightHit   , distance);
         l = meshCollider.Raycast(left    , out leftHit    , distance);
         u = meshCollider.Raycast(up      , out upHit      , distance);
         d = meshCollider.Raycast(down    , out downHit    , distance);
         f = meshCollider.Raycast(forward , out forwardHit , distance);
         b = meshCollider.Raycast(back    , out backHit    , distance);
 
         if(r&&l&&u&&d&&f&&b) {
 
             if(ConcaveHull(right,rightHit))          In = false;
             else if(ConcaveHull(left,leftHit))       In = false;
             else if(ConcaveHull(up,upHit))           In = false;
             else if(ConcaveHull(down,downHit))       In = false;
             else if(ConcaveHull(forward,forwardHit)) In = false;
             else if(ConcaveHull(back,backHit))       In = false;
             else { In = true; concaveHull = false; }
 
         } else In=false;
     
     }
 
 }
Comment
Add comment · Show 1 · 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 K0ndor · Jul 25, 2017 at 03:07 PM 0
Share

It works very well for non-convex mesh colliders - contrary to Physics.CheckSphere. Just what I was looking for!

avatar image
3

Answer by tyjkenn · Jul 03, 2013 at 10:43 PM

Physics.CheckSphere is what you need. Just use a small test sphere like so:

 if(Physics.CheckSphere(testPosition,.0001)){
     //It is in a collider
 } else{
     //It is not in a collider
 }


Comment
Add comment · Show 1 · 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 olejuer · Feb 19, 2020 at 11:23 AM 0
Share

This does only work if the sphere intersects the surface of a mesh and not if it is fully inside.

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

8 People are following this question.

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

Related Questions

Using CombineMesh with point topology? 0 Answers

Test if point is in Collider/Trigger 10 Answers

Remove part of a mesh or points of pointcloud 1 Answer

transform.LookAt (target) mesh anchor point 1 Answer

Weird coordinates when cllicking on collider and convert to mesh point 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