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 CaioRosisca · Jul 05, 2017 at 09:24 PM · c#unity 5collisionphysicscylinder

How to Physics.CheckCylinder?

Just like Physics.CheckCapsule or Physics.CheckBox, what I need is a method that can check for collisions on a Cylinder area. Is there anything like that? Or how can I achieve it?

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
2
Best Answer

Answer by Bunny83 · Jul 05, 2017 at 10:09 PM

Unity doesn't have a cylinder collider type. That's mainly because a cylinder is more difficult to describe as a mathematical formula. A capsule is easier since it's just two connected spheres. The best workaround is to use a combination of a capsule and a box.

Make sure the box collider is actually large enough to enclose the whole cylinder section. Also make sure the "caps" of the capsule are not included. Something like this:

Cylinder

This should do what you want:

 public static bool CheckCylinder(Vector3 aStart, Vector3 aEnd, float aRadius, int aLayerMask, QueryTriggerInteraction aQueryTriggerInteraction)
 {
     if (!Physics.CheckCapsule(aStart, aEnd, aRadius, aLayerMask, aQueryTriggerInteraction))
         return false;
     Vector3 dir = aEnd - aStart;
     return Physics.CheckBox(aStart + dir * 0.5f, new Vector3(aRadius, aRadius, dir.magnitude*0.5f), Quaternion.LookRotation(dir), aLayerMask, aQueryTriggerInteraction);
 }

This let you specify the top center point and the bottom center point of your cylinder and the radius. It will only return true when an object overlaps with both colliders.

It's not perfect as it has some edge cases where a large object at a 45! angle might touch both colliders but is still outside the cylinder. You can't really get a perfect match with the available methods. However you can increase the precision by using another box that is 45° rotated along the cylinder axis. That would "flatten" the edges of the two boxes.

Cylinder2

 public static bool CheckCylinder(Vector3 aStart, Vector3 aEnd, float aRadius, int aLayerMask, QueryTriggerInteraction aQueryTriggerInteraction)
 {
     if (!Physics.CheckCapsule(aStart, aEnd, aRadius, aLayerMask, aQueryTriggerInteraction))
         return false;
     Vector3 dir = aEnd - aStart;
     Quaternion q = Quaternion.LookRotation(dir);
     if (!Physics.CheckBox(aStart + dir * 0.5f, new Vector3(aRadius, aRadius, dir.magnitude*0.5f), q, aLayerMask, aQueryTriggerInteraction))
         return false;
     q = Quaternion.AngleAxis(45, dir) * q;
     if (!Physics.CheckBox(aStart + dir * 0.5f, new Vector3(aRadius, aRadius, dir.magnitude*0.5f), q, aLayerMask, aQueryTriggerInteraction))
         return false;
     return true;
 }

Of course this "rotation" of the boxes could be made smaller and smaller and you would use more and more boxes to increase precision.

edit Probably the best solution would be this one:

 public static bool CheckCylinder(Vector3 aStart, Vector3 aEnd, float aRadius, int aLayerMask, QueryTriggerInteraction aQueryTriggerInteraction)
 {
     if (!Physics.CheckCapsule(aStart, aEnd, aRadius, aLayerMask, aQueryTriggerInteraction))
         return false;
     Vector3 dir = aEnd - aStart;
     Quaternion q = Quaternion.LookRotation(dir);
     Quaternion q2 = Quaternion.AngleAxis(45f, dir);
     Vector3 size = new Vector3(aRadius, aRadius / (1f + Mathf.Sqrt(2f)), dir.magnitude * 0.5f);
     for (int i = 0; i < 4; i++)
     {
         if (Physics.CheckBox(aStart + dir * 0.5f, size, q, aLayerMask, aQueryTriggerInteraction))
             return true;
         q = q2 * q;
     }
     return false;
 }

It uses one capsule and 4 boxes. The 4 boxes actually form an octagon on the outside of the capsule. In this case we need a collision with the capsule and any of the boxes. I can't attach another image so i post it as comment below.


cylindercollidercheck.png (31.0 kB)
cylindercollidercheck2.png (31.9 kB)
Comment
Add comment · Show 2 · 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 Bunny83 · Jul 05, 2017 at 11:05 PM 1
Share

Hmm, seems i can't upload an image in a comment. So here it is on my dropbox:

avatar image CaioRosisca · Jul 06, 2017 at 03:24 PM 0
Share

Solution worked really well but needed an adjustment, the capsule's caps should extrapolate from Start and End positions. From my tests the code above had the capsule the same size as the boxes.

  if (!Physics.CheckCapsule(aStart, aEnd, aRadius, aLayer$$anonymous$$ask, aQueryTriggerInteraction))

should be replaced by

 if( !Physics.CheckCapsule(aStart- (dir.normalized*radius), aEnd+ (dir.normalized * aRadius), radius, aLayer$$anonymous$$ask, aQueryTriggerInteraction))

Just out of curiosity, whats the "radius / (1 + sqrt(2))" formula for?

Here's the function as I'm using now, tested it on all possible ways and it's working perfectly. The code includes some DebugDraw for testing purposes, just ignore it.

 public static bool CheckCylinder(Vector3 start, Vector3 end, float radius, int layer$$anonymous$$ask, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)
     {
         Vector3 dir = end - start;
 
         bool overlappingCapsule = Physics.CheckCapsule(start - (dir.normalized*radius), end + (dir.normalized * radius), radius, layer$$anonymous$$ask, queryTriggerInteraction);
         
         DebugExtension.DebugCapsule(start - (dir.normalized * radius), end + (dir.normalized * radius), overlappingCapsule ? Color.red : Color.green, radius);
         
         //If not colliding with capsule isn't colliding at all
         if (!overlappingCapsule)
             return false;
 
         //If here it's inside the capsule area
 
         Quaternion q = Quaternion.LookRotation(dir);
         Quaternion q2 = Quaternion.AngleAxis(45f, dir);
         Vector3 size = new Vector3(radius, radius / (1f + $$anonymous$$athf.Sqrt(2f)), dir.magnitude * 0.5f);
         
         bool boxOverllaping;
 
         for (int i = 0; i < 4; i++)
         {
             boxOverllaping = Physics.CheckBox(start + dir * 0.5f, size, q, layer$$anonymous$$ask, queryTriggerInteraction);
             
             DebugExtension.DebugBox(start + dir * 0.5f, size, q, boxOverllaping ? Color.red : Color.green);
 
             //Inside capsure area and inside box area
             if (boxOverllaping)
                 return true;
 
             q = q2 * q;
         }
 
         //Inside capsure area but not inside any box. This means it's inside the capsule "caps" area.
         return false;
     }
avatar image
2

Answer by Kishotta · Jul 05, 2017 at 10:01 PM

There is no CheckCylinder, but you could check for a capsule that extends beyond the cap of the cylinder, and then also check for a box that aligns with the caps of the cylinder. If both checks pass, you have a cylinder collision.

alt text


checkcylinder.png (4.0 kB)
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

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

Handling colliders on hundreds of asteroids (not procedural asteroids) ...URGENT 3 Answers

OnMouseDown with colliders behind the object being clicked 3 Answers

How to change CC script to Rigidbody script 1 Answer

Objects not colliding twice 0 Answers

Delaying Physics Calculations (Friction) 1 Answer


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