• 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
1
Question by Tohron · Jan 16, 2017 at 06:54 PM · colliderbounds

How to check if a point is in a rotated BoxCollider?

I've got pathing code that involves checking if an enemy is in an area that it isn't supposed to be. These areas are defined by boxes with IsTrigger colliders, and these boxes are often rotated to match with the desired areas. The current code is this:

 private bool IsInExclusionZone(out int index)
     {
         for (int i = 0; i < regionTracker.exclusionZones.Length; i++)
         {
             // PROBLEM: Bounds are in a worldspace box
             // Listing BoxCollider instead changes nothing
             if (regionTracker.exclusionZones[i].GetComponent<Collider>().bounds.Contains(transform.position))
             {
                 index = i;
                 return true;
             }
         }
         index = -1;
         return false;
     }


The problem is, if one of the exclusion zones is say, rotated 45 degrees on the y axis, a point that is outside the collider, but still within the worldspace bounds of the collider, will register as being "contained". How do I alter this code so that it only returns true when transform.position is inside the actual collider?

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

2 Replies

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

Answer by Glurth · Jan 17, 2017 at 12:26 AM

The Bounds returned by a Collider is in world-space coordinates, and is axis aligned. So, if you rotate the collider, the bound's various edge-size may change, but it will remain axis aligned.

So if you want to see if a point is contained within rotated collider (not it's bounds), I would recommend using the collider's raycast function. Set the origin as the world-space point you want to test, and the second point as the world-space center of the bounds. If no collision is detected you are inside. (works consistently for convex colliders only.)

e.g.

     Vector3 posToCheck = positiontoCheck.position;
     Vector3 offset = colliderToCheck.bounds.center- positiontoCheck.position;
     Ray inputRay = new Ray(posToCheck,offset.normalized);
     RaycastHit rHit;
     Debug.DrawRay(posToCheck, offset);
        
     if (!colliderToCheck.Raycast(inputRay, out rHit, offset.magnitude * 1.1f))
         inside = true;
     else
         inside = 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 Tohron · Jan 17, 2017 at 01:51 AM 0
Share

Thanks, that appears to be working fine!

avatar image
0

Answer by mkgame · Dec 28, 2018 at 11:26 AM

Sometimes we just don't want to have a collider, because we have limited layers. I would recommend to set a bound in editor mode or calculate the bounds in Awake from all Renderers (except the ParticleSystem and LineRenderer). Then rotate the point back, which should be checked, by the rotation of your GameObject. The pivot point for rotation must be the GameObject pivot point. Then check this point by bounds.Contains(...).

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

71 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

Related Questions

Converting Bounds.size to GUI? 0 Answers

How to project the position of a point relative to a rotated collider using its bounds? 1 Answer

Percentage of Collider within a Trigger Area (C#) 1 Answer

Set bounds of collider added at runtime 1 Answer

Can't convert Bounds from world coordinates to local coordinates 6 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