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:
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: Unity - Manual: Continuous collision detection (CCD) (3D) Unity - Manual: Introduction to Rigidbody 2D (2D)
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.
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.