Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

2D Character (circle) "slips" through tiles. Sometimes. Frequently. Video attached.

Discussion in 'Physics' started by msh91, Oct 30, 2019.

Thread Status:
Not open for further replies.
  1. msh91

    msh91

    Joined:
    Apr 22, 2017
    Posts:
    37
    I have a character which is a ball using a polygon collider 2d (with rigidbody), and my map is made from tiles, using tilemap collider 2d and composite collider 2d.

    In order to move my ball, I'm setting the rigidbody velocity directly.
    e.g.
    ```
    Rigidbody2D.velocity = new Vector2(inputX * Speed * Time.fixedDeltaTime, Rigidbody2D.velocity.y);
    ```
    I also tried using AddForce(), but it doesn't solve the problem.

    Sometimes everything is fine, but sometimes the character just falls through the tiles.

    What am I missing?

    Attached video showing an example of slipping through:

     
    Last edited: Oct 31, 2019
  2. ShervinM

    ShervinM

    Joined:
    Sep 16, 2017
    Posts:
    67
    How are you making the ball move? Rb.MovePosition? Forces? Transform.Translate/position?
     
  3. msh91

    msh91

    Joined:
    Apr 22, 2017
    Posts:
    37
    ha I should of wrote that, my bad.

    I'm setting the rigidbody velocity directly.
    e.g.
    ```
    Rigidbody2D.velocity = new Vector2(inputX * Speed * Time.fixedDeltaTime, Rigidbody2D.velocity.y);
    ```

    I'll also edit the original pot to add it; it should of been there in the first place.

    p.s.
    I tried to use AddForce() instead. However, the issue still happens in that case as well
     
  4. ShervinM

    ShervinM

    Joined:
    Sep 16, 2017
    Posts:
    67
    No worries :)

    So my first though was that perhaps you may have been setting the position directly (Rigidbody.MovePosition or Transform.Position). With a delta large enough, you could accidentally place your object within (or even past) a collider.

    This is clearly not the case as you're setting the velocity / adding forces.

    My next suggestion would be to try to set your collision detection (on the Rigidbody of your sphere) to continuous instead of Discrete (default). Basically continuous looks at the first contact point of a collision and tries to correct moving objects from accidentally passing through other colliders by moving them back to that first contact point.. This often happens when objects are moving really fast or if you're dealing with thin colliders. You can read more about it here:
    https://docs.unity3d.com/Manual/ContinuousCollisionDetection.html (3D)
    https://docs.unity3d.com/Manual/class-Rigidbody2D.html (2D)

    Give that a shot and see if it works
     
  5. msh91

    msh91

    Joined:
    Apr 22, 2017
    Posts:
    37
    I didn't say that either, but it's already at continuous - the problem still happens.
    I'm not sure what info needed, so sorry for not specifying ahead.

    Also note someone at the Discord channel gave me a solution, sort of:
    The Tilemap Collider 2d has a geometry type, which is by default set to outline.
    I set it to polygon - and now the character doesn't fall through.

    I still feel like there's something going on here, not to mention it's definitely a hit in performance, so if anyone knows about a better solution I would love to know. But for now anyway, it works.
     
  6. WayneJP

    WayneJP

    Joined:
    Jun 28, 2019
    Posts:
    44
    Similar problem here, use geometry type, which is set to outline, cause rigidbody2d.velocity equals to (-5.02269e-07, 0) when rgbd2d is not moving. Set to polygon, problem solved.

    Really want ot know why.
    Here is my question link:https://forum.unity.com/threads/my-...-other-project-prints-0.1336931/#post-8445725
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,512
Thread Status:
Not open for further replies.