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
1
Question by Darth_Biomech · Dec 03, 2019 at 11:48 AM · 2dcollider2dgaps

why is there's a gap between colliders2d?

Despite the colliders being aligned to be pixel-perfect, there's still a weird gap between the player's collider and the ground's collider. I guess I could just make the player's collider a bit smaller (or shift the sprite renderer one pixel down), but it feels like not-solving the problem. Unless it's actually the desired behavior of colliders for [insert reason here]. alt text

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

Answer by Ermiq · Dec 03, 2019 at 01:12 PM

It might depend on what you use for character controls.


For rigidbody based character the gap could be changed by setting up physics default contact offset in Edit->Project Setting->Physics(or Physics2D)->Collision detection offset. The higher value you set, the more colliders will intersect each other before the engine will detect collision between them.


For CharacterController it's controller.SkinWidth property. Unity suggests to set it to 10% of the controller radius. Just like with physics contact offset, it determines how far colliders should intersect each other before the collision detection.


For NavMeshAgent driven character it depends on the nav mesh. There's an option in nav mesh baking setting to make it more presice and to fit the terrain better, without that setting nav mesh could be significantly higher above the ground.

Comment
Add comment · Show 5 · 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 Darth_Biomech · Dec 03, 2019 at 02:25 PM 0
Share

Thanks. Collision detection offset was the guilty one, but trying to set it to 0 caused bugs with the controls (character lost ability to detect when they're standing on the ground), so I had to go with the value of 0.0075 for now. It's not perfect, but it gives the character an offset of exactly 1 pixel from the surface so I could just edit the sprite, I guess.

avatar image Ermiq Darth_Biomech · Dec 03, 2019 at 02:52 PM 1
Share

To prevent this grounded/notGrounded glitch you have to include the physics contact offset in your ground check code. Usually, with physics based character (with non-kinematic rigidbody) the ground check is done by casting a sphere (or circle in 2D), and in the cast code you need to set the sphere start point height as transform.position + Vector3.up * (characterRadius + Physics.defaultContactOffset), the sphere radius to characterRadius - Physics.defaultContactOffset, and the distance to Physics.defaultContactOffset. With this casting setup it won't glitch.

avatar image Darth_Biomech Ermiq · Dec 03, 2019 at 03:13 PM 0
Share

I'm vandalizing the stock 2d template for $$anonymous$$e, so there the collision check is done in this way (as far as I can tell:

     protected virtual void Start()
     {
         contactFilter.useTriggers = false;
         contactFilter.SetLayer$$anonymous$$ask(Physics2D.GetLayerCollision$$anonymous$$ask(gameObject.layer));
         contactFilter.useLayer$$anonymous$$ask = true;
     }
     ///-------Code omitted------///

     void Perform$$anonymous$$ovement(Vector2 move, bool y$$anonymous$$ovement)
     {
         var distance = move.magnitude;

         if (distance > $$anonymous$$$$anonymous$$oveDistance)
         {
             //check if we hit anything in current direction of travel
             var count = body.Cast(move, contactFilter, hitBuffer, distance + shellRadius);
             for (var i = 0; i < count; i++)
             {
                 var currentNormal = hitBuffer[i].normal;

                 //is this surface flat enough to land on?
                 if (currentNormal.y > $$anonymous$$GroundNormalY)
                 {
                     IsGrounded = true;
                     // if moving up, change the groundNormal to new surface normal.
                     if (y$$anonymous$$ovement)
                     {
                         groundNormal = currentNormal;
                         currentNormal.x = 0;
                     }
                 }
                 if (IsGrounded)
                 {
                     //how much of our velocity aligns with surface normal?
                     var projection = Vector2.Dot(velocity, currentNormal);
                     if (projection < 0)
                     {
                         //slower velocity if moving against the normal (up a hill).
                         velocity = velocity - projection * currentNormal;
                     }
                 }
                 else
                 {
                     //We are airborne, but hit something, so cancel vertical up and horizontal velocity.
                     velocity.x *= 0;
                     velocity.y = $$anonymous$$athf.$$anonymous$$in(velocity.y, 0);
                 }
                 //remove shellDistance from actual move distance.
                 var modifiedDistance = hitBuffer[i].distance - shellRadius;
                 distance = modifiedDistance < distance ? modifiedDistance : distance;
             }
         }
         body.position = body.position + move.normalized * distance;
     }


What should be changed in this case?

Show more comments

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

233 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

Related Questions

All of my doors are opening at the same time. Why? 0 Answers

Object hits another but doesnt bounce off again. 0 Answers

Find next corner of a 2d pollygon collider 0 Answers

How to eliminate gaps between 2d colliders? 2 Answers

Unity2D - OnTriggerEnter2D not working 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