Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 Ermiq · Aug 18, 2018 at 05:40 PM · spherecastground detectioncapsulecollider

Spherecast for ground check glitch

Hi there.
I have a character controller which is have a rigidbody (use gravity, non-kinematic) and a capsule collider (non-trigger, height = 1.8, center = 0.9).

I use the following code to detect if the character is on the ground:

 private bool GroundCheck (float groundCheckHeight, out bool result)
 {
     Physics.SphereCast (this.transform.position + Vector3.up * CharCollider.radius,
         CharCollider.radius, Vector3.down, out groundHitInfo,
         groundCheckHeight, LayersHelper.LayerExceptEnemiesPlayerAndUI, 
         QueryTriggerInteraction.Ignore);
     Debug.Log ("Dist: " + groundHitInfo.distance + ", collider: " + groundHitInfo.collider);
     return result =
         groundHitInfo.distance <= groundCheckHeight
             ? true : false;
 }

LayersHelper is just a class which contains constants for layers.
LayerExceptEnemiesplayerAndUI = ~(1 << 10) | (1 << 5) | (1 << 8)

With this code I'm getting result twiching from true and debug prints that collider is a Terrain, if I move the character or even turn the camera, result swithes to false and collider in debug is empty.
If I use Physics.AllLayers then result is always true.
If I use ~(1 << 10)(layer 10 is a Player layer and character (with all children is set to this layer) the result is still always true even if I jump, and again collider is empty.

And no matter what check distance, is it 0.1f, 0.001f, 0.3f, the result still that unadequate.

So, does anyone know what is going on here? How is it possible so collision is happen but there's no colliders?

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

1 Reply

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

Answer by Ermiq · Aug 18, 2018 at 06:55 PM

Well, the problem is solved!
For anyone who interested: the thing is, Unity uses some offset for collision detection. When two colliders intersect each other, Unity detects if they went inside each other more than this offset allows. If so, collision detection happens.
You can find this offset in Edit -> Project settings -> Physics (or Physics2D if you use 2D). It's called Detect Contact Offset and it's 0.01 by default.
So, to solve my problem, I just substructed this offset off the sphere size:

 Physics.SphereCast (this.transform.position +
     // here, to make the sphere start position higher by this offset
     Vector3.up * (CharCollider.radius + Physics.defaultContactOffset),
     // and here to make the sphere radius lower by this offset
     CharCollider.radius - Physics.defaultContactOffset,
     Vector3.down, out groundHitInfo, maxCheckHeight,
         LayersHelper.LayerExceptEnemiesPlayerAndUI, QueryTriggerInteraction.Ignore);

Also, why did RaycastHit return no colliders? Because if there's no collision at the given range, then RaycastHit result has .collider = null; .transform = null; distance = 0; !
That's all. Now the sphere acts as I expect, collision detection works like a charm.
Happy coding, all!

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 Mephalrith · Mar 04, 2019 at 12:00 AM 0
Share

I tried all kinds of different casts and collisions, and this is what finally worked for me! Thank you so much!

avatar image diliupg · Mar 31, 2021 at 05:49 AM 0
Share

Thank you very much! I wasted so much time looking for this solution and NOT ONE video on Youtube explains this simple thing. I'm sure you have gone ahead and become a great developer. Many thanks once again!

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

89 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

Related Questions

Inconsistent results when doing line/sphere casts? 0 Answers

Checking if grounded using SphereCast -- Acting like Raycast 1 Answer

Tree Colliders are not Triggers 1 Answer

Mobile Build and Playmode Terrain Collision Disparity 0 Answers

Raycast Never Hits Anything?! 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