• 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 mine, 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 > min$$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 > minGroundNormalY)
                 {
                     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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

How can I make it so that the Level Reloads whenever the Player touches certain Colliders or Triggers? 1 Answer

Object: collider to floor, trigger to player 1 Answer

Polygon Collider 2D: prevent automatic generation 2 Answers

2d rigidbody falling from the collider when it is moved 2 Answers

Making Rigidbodies to interact but allow them to go one through the other 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges