I'm currently working on a simple 2D Physics-Based Platformer Project. However, I ran into the following issue:
I'm using a Tilemap and a TilemapCollider2D to display the world. When I move the player object, it sometimes gets stuck on the edges between the different squares of the TilemapCollider2D.
Here is a little gif showcasing my problem. Please note that I'm continually pressing A/D to move over the surface. The stopping is the bug I mentioned:
enter image description here

Here is my player code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour

Rigidbody2D rb2d;

[Header("Background Variables")]
public float speed = 5.0f;
public float maxSpeed = 10.0f;
public float jumpStrength = 10.0f;
public float friction = 2.5f;

public bool isGrounded = false;
public int walling = 0; // 0 = no wall collision; 1 = left side collision; 2 = right side collision

private void Start()
    rb2d = GetComponent<Rigidbody2D>();

private void LateUpdate()
    if(rb2d.velocity.x > maxSpeed)
        rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
    else if(rb2d.velocity.x < -maxSpeed)
        rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);

    if(Input.GetAxisRaw("Horizontal") != 0.0f)
        rb2d.AddForce(speed * Vector2.right * Input.GetAxisRaw("Horizontal"), ForceMode2D.Impulse);
        rb2d.velocity = Vector2.Lerp(rb2d.velocity, new Vector2(0.0f, rb2d.velocity.y), friction);

        rb2d.velocity = new Vector2(rb2d.velocity.x, jumpStrength);

// These two methods currently have no effect, but I plan on extending them later
public void SetGrounded(bool isGrounded)
    this.isGrounded = isGrounded;

public void SetWalling(int walling)
    this.walling = walling;

My player uses a Rigidbody2D and BoxCollider2D.
Is there any good way to prevent this from happening or at least a workaround?

Thank you in advance!

  • \$\begingroup\$ Have you read previous questions about getting stuck at the borders of box colliders? ("tile stuck" makes a good search query for turning up a bunch of them) This is a common physics engine quirk you can learn more about in this GDC talk by Bennett Foddy \$\endgroup\$
    – DMGregory
    Mar 3 '19 at 14:17
  • \$\begingroup\$ @DMGregory I researched this issue before posting but it seems I simply used the wrong terms for search, sorry. I didn't know that this is actually a common problem. I think I'll try to see if I can manage to merge the Tilemap Collider Squares into more connected ones. \$\endgroup\$ Mar 3 '19 at 14:49

After trying to write a code that merges the squares effectively (especially for large Tilemaps) both horizontally and vertically I decided to look if there is a better suited collider than the PolygonCollider2D which I've been using. Then I stumbled across a certain component and now I feel really stupid...

The trick to avoid the problem I mentioned in my question involves absolutely 0 lines of code.

  1. For the TilemapCollider2D check the box "Used by Composite".
  2. Then add the Component CompositeCollider2D (Found under Physics2D). This will automatically add a Rigidbody2D to your object if it doesn't have one already.
  3. Change the "Body Type" of the Rigidbody2D to Kinematic unless you want physical interaction with the tilemap.
  4. Profit.

I did some testing and it seems like my problem is solved now. If I should run across another error with this method, I'll update this answer accordingly.

  • \$\begingroup\$ Now my character falls into the ground a bit every time I land on the platform \$\endgroup\$
    – Yuval Amir
    Jan 9 at 22:31

Two things:

  • Using LateUpdate instead of FixedUpdate might be the culprit - try FixedUpdate.
  • It may also be related to the box collider on your players character - try sphere/capsule colliders instead.
  • \$\begingroup\$ Using FixedUpate sadly only adds a chance to the jump button not working properly and doesn't fix the situation. Edit: I've also already tested the problem with a single stretched out collider (which would be very impractical for a Tilemap), in which case it's gone. This would mean that the entire problem comes from the way a Tilemap divides its collider into squares. Also using sphere/capsule colliders causes my player character to jump uncontrollable when moving between the tiles. All this behaviour would indicate a height difference between the tiles, which does not exist in reality \$\endgroup\$ Mar 3 '19 at 14:40
  • \$\begingroup\$ I see. I guess I can't be of any help then \$\endgroup\$
    – Bartimaeus
    Mar 3 '19 at 14:54
  • \$\begingroup\$ Thank you for trying anyways, though :) \$\endgroup\$ Mar 3 '19 at 15:00
  • \$\begingroup\$ Using a sphere collider fix this issue for me \$\endgroup\$ Dec 10 '19 at 23:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.