Search Unity

2D Character Colliders and Ground Control problem

Discussion in 'Physics' started by Sharpin, Aug 17, 2019.

  1. Sharpin

    Sharpin

    Joined:
    May 26, 2019
    Posts:
    10
    Hi all, I have some problems with colliders for about 3 days. I think it's time to ask someone else about it. I have 2 colliders: 1 - box - attached to Player game object and second -Circle - attached to a child of player game obejct. My problem you can see on the screenshoot. Circle collider is a "ground controller", sometimes it's possible to make character falling becouse circle collider doesn't touch but box collider does. Making circle collider bigger solves the problem but also adds another (for me even worse) - When we jump on a platform or corner with a collider the round shape of player circle collider and sharp shape of tile box collider add to player some force ( at least it looks like adding force) and character flies further than it should. Of Course box collider can't detect ground becouse then when you touch ground from left or right it makes ground control true even thought character is falling and just simply touched the platform from the side. Maybe there is a best colliders set for a platformer game or another way to solve it?

    Thanks in advnace :)
     

    Attached Files:

  2. Aristonaut

    Aristonaut

    Joined:
    May 4, 2019
    Posts:
    15
    Yeah, the Unity physics will transfer force when you glance off at an angle. The only way I have really found to handle this is to cache my calculated velocity in FixedUpdate, then stomp over what unity did by reverting to the calculated velocity in "WaitForFixedUpdate" or the beginning of FixedUpdate. This does remove all friction, though, so you'd have to calculate that yourself in FixedUpdate, or perhaps just kill unity physics on stationary objects. Overall, it's a difficult one, since the physics engine isn't really written for platformers with really tight control over the physics.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
  4. Sharpin

    Sharpin

    Joined:
    May 26, 2019
    Posts:
    10
    Capsule collider makes it fly too, just like circle collider. BoxCollider2D is needed here to avoid this added "force" of round colliders.
     
  5. Sharpin

    Sharpin

    Joined:
    May 26, 2019
    Posts:
    10
    Thanks for reply. That sounds pretty hard. Could you post some code of this way? Maybe when i see this it will become clearer. Otherwise maybe i will find or create some mechanics to avoid this somehow.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    What do you mean by added force? There's no added force from colliders. They're either colliding or not colliding.
     
  7. Sharpin

    Sharpin

    Joined:
    May 26, 2019
    Posts:
    10
    I just don't know how to describe it precisely. Just after jumping on a platform with box collider / tilemap collider with attached ONLY circle or capsule collider to a character sometimes the character "slides" up like something added him some force. I know that it isn't force exactly but it's the closest term i think to describe it without a video. But beyond that, i've added poligon collider instead of box collider and contorted a little it's bottom corners that don't add that much of "force" of maybe somekind of "slip" while jumping on a platform but still it makes player fall when trying to do same thing as on the attached screenshot.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Might want to post a video describing what behaviour you expect, I still don't follow. Alll I can see in the video is the player "floating" on the edge of a platform but that seems to be because the collider doesn't seem to represent the visuals, maybe because of bad positioning or even animation with the legs up. Not sure.
     
  9. Sharpin

    Sharpin

    Joined:
    May 26, 2019
    Posts:
    10
    Problem on the screen i've kinda solved. But i was talking about second issue when round shape colliders makes character go a little up. Using only round shape colliders or make circlecollider bigger and outside the boxcollider bounds were my first thoughts to solve issue on the screenshoot but then second problem with "force" appeared and that is what i was talking about. Main question was about floating - sure. But then I just get to the second issue with round shape colliders. Now everything seems to work with box collider and circle trigger to detect ground (I didn't wanted to use this way becouse of problems with collision while moving between a boxcollider and a tilemap collider - but now i don't have this problem so it might be it). Sorry if i wasn't clear to understand :)
     
  10. Aristonaut

    Aristonaut

    Joined:
    May 4, 2019
    Posts:
    15
    Here is an example of straight-up nullifying all force transfers that occur in the unity side. You essentially get 1 physics tick of transfer/settling, then it's removed.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class GlanceRemover : MonoBehaviour
    4. {
    5.     private Rigidbody2D rigbo;
    6.     private void Start()
    7.     {
    8.         rigbo = GetComponent<Rigidbody2D>();
    9.     }
    10.  
    11.     private Vector2 preCollisionVelocity;
    12.     private void FixedUpdate()
    13.     {
    14.         // ...Other physics calculations...
    15.         preCollisionVelocity = rigbo.velocity;
    16.     }
    17.  
    18.     private void OnCollisionEnter2D( Collision2D collision )
    19.     {
    20.         NullifyGlancing( collision );
    21.     }
    22.  
    23.     private void OnCollisionStay2D( Collision2D collision )
    24.     {
    25.         NullifyGlancing( collision );
    26.     }
    27.  
    28.     private void NullifyGlancing( Collision2D collision )
    29.     {
    30.         rigbo.velocity = preCollisionVelocity;
    31.         // add more smarts here for ground and stuff
    32.     }
    33. }
    Now, this is far from a complete example. You will need to expand "NullifyGlancing" to check for ground and moving platforms and things. It's not going to be easy to use the stock physics engine to do a platformer. I am still wresting with my implementation, but this jumped me fairly close to what I really want.

    Oh, and you'll want a capsule collider to use with this one. For ground checks, I would check the floor normal in the collision callbacks. You may need to debounce it, though, as they can trigger after you "jump". When using stock physics, it's best to assume they are messy and work a bunch of slop room in.
     
    Sharpin likes this.