- Home /
Player ignores tilemap collider on fall
I created a very simple project to reproduce this problem. I can't understand what was happening, it seems like a bug to me, but I'm not sure. I created this script, so the player can jump infinity times, and at a certain height, when the player falls, the collision with tilemap collider 2D is ignored, with or without composite. I try to use composite because I thought he was passing between the squares, but with composite, it's even worse, the player ignores in lower height.
This project has 2 tilemaps, 1 with composite, and another without. The white is with, and the blue is without. The tilemap without composite is a simple tilemap with Tilemap Collider 2D. To create a tilemap with composite, I also added a Tilemap Collider 2D, but I set Used By Composite, and I add a Composite Collider 2D, which automatically adds a Rigidbody 2D on tilemap, so I set this Rigidbody 2D as Kinematic.
My PlayerController script:
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerController : MonoBehaviour
{
private Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
Walk();
Jump();
Restart();
}
void Walk()
{
float horizontalMovement = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(horizontalMovement * 10, rb.velocity.y);
}
void Jump()
{
if (Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(new Vector2(0, 30f), ForceMode2D.Impulse);
}
}
void Restart()
{
if (Input.GetKeyDown(KeyCode.R))
{
SceneManager.LoadScene("SampleScene");
}
}
}
Besides that, the gravity scale on player Rigidbody2D is 10, but I don't think this can be a reason for that.
I post this project on GitHub, which you can see here.
Answer by The-Peaceful · Apr 30, 2021 at 08:28 PM
Hi, I am just guessing here because I don't know the settings of your Rigidbody component, but it sounds very much like the Rigidbody is using the 'Discrete' collision detection mode to me. When using this collision mode with fast moving objects, the problem you are describing might happen. For a more accurate collision detection on fast moving objects use the 'Continuous' collision mode instead, though this is less performant than the 'Discrete' mode. Hope it helps :D
See the difference between the collision modes: https://www.youtube.com/watch?v=hKAYDg9Rswk
Read up more on the collision modes: https://docs.unity3d.com/Manual/class-Rigidbody2D.html
Thanks for the answer, and yes, you are right, I was using Discrete collision on Rigidbody2D. I change to Continuous and it seems to work correctly now.