Search Unity

2D Tilemap bug

Discussion in '2D' started by bl4ckfoot, Oct 15, 2021.

  1. bl4ckfoot

    bl4ckfoot

    Joined:
    Sep 16, 2021
    Posts:
    7
    I found a bug regarding the tilemap / tilemap collider 2d. I'm surprised no one is talking about this issue so I'm not sure if it is a mistake on my part, espacially because I'm pretty new to unity. I asked the same question on unity answers: https://answers.unity.com/questions/1865631/why-are-tilemaps-so-bad.html. No one answered so far.

    Steps to reproduce:
    1. Create tilemap with tilemap collider 2d
    2. Create object with rigidbody 2d and capsule collider 2d / box collider 2d
    3. Let it move along a straight surface of a tilemap

    Actual Result:
    Using the box collider 2d the object will eventually get stuck on the tilemap
    Using the capsule collider 2d the object will start bouncing (even if the object is told to keep the y velocity when moving)

    Expected Result:
    Object moves along the tilemap.

    Reproducible on:
    2019.3.0f3 / didn't test it anywhere else

    Environment:
    Occurring on Windows 10 / didn't test it anywhere else

    I tried using a composite collider 2d and also make some adjustments to the tilemap. Nothing helped.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    How are you "letting it move?"

    If you do so ANY other way than a) under its own ballistic momentum or b) by calling
    .MovePosition()
    on the Rigidbody instance, you will see EXACTLY the glitchy behavior you see above.

    Are you using
    transform.Translate()
    or setting
     transform.position
    directly? That is a strict no-no with the physics system because it completely bypasses the physics system and then it freaks out trying to catch up with the new reality you have surprised it with.
     
    bl4ckfoot likes this.
  3. bl4ckfoot

    bl4ckfoot

    Joined:
    Sep 16, 2021
    Posts:
    7
    Sorry for not clarifying what technique i use to move the object. I get this behavior by setting the velocity or adding force on the rigidbody.

    And you were right. First off thank you. I do not get the issue when using .MovePosition(). The problem is, that raises a couple of questions. Why is this happening in the first place ? Isn't a rigidbodys main purpose to handle exactly that ? And why is this issue only occuring on a tilemap ? And also if I should be using .MovePosition() to move a rigidbody then why use a rigidbody in the first place ? I use a rigidbody for the convience to be able to just tell it the velocity and it does the rest itself (And of course also for the collision detection and handling).

    It would be a lot of work to transform my whole project from setting the velocity to using .MovePosition().
     
  4. Zephus

    Zephus

    Joined:
    May 25, 2015
    Posts:
    356
    The short answer is: that's what Composite Collider 2D is for.

    The long one: If you're using a Tilemap Collider then individual tiles get colliders in form of their physics shape. If that happens to be a square as big as the tile, then you have a row of individual squares with very, very tiny gaps in between. This is just how the physics engine works, and that's probably what you're getting stuck on. If you use a Capsule Collider, then the player doesn't have any edges that could get stuck anymore.

    This should be fixed if you add the Composite Collider correctly. There is no way to get stuck on gaps that aren't there. In your screenshot it seems like you just checked 'used by composite', but you also need to add a Composite Collider 2D Component and a static Rigidbody2D to the tilemap or the option won't do anything. It seems you added it to the player? You'll see in the scene view if the entire shape has become one giant collider, which should make it impossible to get stuck.

    I don't know what you mean by bouncing, but try what I wrote above with a Box collider 2D first. If that doesn't work, maybe freeze rotation under "Constraints" in the Rigidbody2D.
    By the way, Time.fixedDeltaTime is exactly that - a constant value. It's the fixed timestep in the physics settings that's probably 0.02, so you're basically just making the number smaller. This is useful when you manipulate Time Scale, but in your example it doesn't do anything else.
     
    Thalon4 and bl4ckfoot like this.
  5. bl4ckfoot

    bl4ckfoot

    Joined:
    Sep 16, 2021
    Posts:
    7
    Thank you so much. This was the problem and it completely makes sense. Every page I found discussing this issue said that you need to add a composite collider to the object interacting with the tilemap but that's nonsense. And also none of my sources said anything about the fact that the tilemap itself also needs a rigidbody.

    I wish you all the best mate.
     
    Last edited: Oct 16, 2021