Jitter with Tilemap Collider's but no Jitter on standard Box Collider
Can't seem to figure why moving into Tilemap Collider's cause a jitter, but moving into Box Collider's work correctly. Here's a video of my issue:
https://www.youtube.com/watch?v=gX__EhUPlUs
I'm using Collider Overlap detection for my project, instead of Adding forces to Rigidbodies. It seems to work correctly with any Box Collider I've placed. I'd be willing to continue doing this, but it's extremely tedious to manually place Box Collider's over every platform/wall, so I tried using Tilemap Collider's to make level design faster and more streamlined.
I noticed as soon as I started adding Tilemap Colliders, I get a lot of jitter in the corners of those platforms. I hear conflicting stories that Tilemap colliders are actually Edge colliders and not boxes, that's why the overlap check doesn't work. But I couldn't find proof of this.
Is there any way to make Tilemap collider's work with how I have my project currently built? Here's my current controller:
using UnityEngine;
[RequireComponent(typeof(BoxCollider2D))]
public class CharacterController2D : MonoBehaviour
{
float speed = 9;
float walkAcceleration = 75;
float airAcceleration = 30;
float groundDeceleration = 70;
float jumpHeight = 2.5f;
private BoxCollider2D boxCollider;
private Vector2 velocity;
public bool grounded;
public int jump = 0;
private void Awake()
{
boxCollider = GetComponent<BoxCollider2D>();
}
private void Update()
{
if (grounded)
{
velocity.y = 0;
if (Input.GetButtonDown("Jump"))
{
velocity.y = Mathf.Sqrt(2 * jumpHeight * Mathf.Abs(Physics2D.gravity.y));
jump++;
}
}
if (!grounded)
{
if (Input.GetButtonDown("Jump") && jump > 0)
{
velocity.y = Mathf.Sqrt(2 * jumpHeight * Mathf.Abs(Physics2D.gravity.y));
jump = 0;
}
}
float acceleration = grounded ? walkAcceleration : airAcceleration;
float deceleration = grounded ? groundDeceleration : 0;
float moveInput = Input.GetAxisRaw("Horizontal");
if ((moveInput > 0.02) || (moveInput < -0.02))
{
velocity.x = Mathf.MoveTowards(velocity.x, speed * moveInput, acceleration * Time.deltaTime);
}
else
{
velocity.x = Mathf.MoveTowards(velocity.x, 0, deceleration * Time.deltaTime);
}
velocity.y += Physics2D.gravity.y * Time.deltaTime;
transform.Translate(velocity * Time.deltaTime);
grounded = false;
Collider2D[] hits = Physics2D.OverlapBoxAll(transform.position, boxCollider.size, 0);
foreach (Collider2D hit in hits)
{
if (hit == boxCollider)
continue;
ColliderDistance2D colliderDistance = hit.Distance(boxCollider);
if (colliderDistance.isOverlapped)
{
transform.Translate(colliderDistance.pointA - colliderDistance.pointB);
if (Vector2.Angle(colliderDistance.normal, Vector2.up) < 90 && velocity.y < 0)
{
grounded = true;
}
}
}
}
}
So just experimenting, but I discovered that if I just added a duplicate tilemap layer (turned off sprite renderers, just leaving colliders), the jitter issue doesn't really happen anymore.
So for some reason tilemap colliders aren't acting as "strong" as a normal box collider?
Answer by lgarczyn · Dec 09, 2019 at 02:37 AM
If you are reimplementing the entire rigidbody system, why not use a Rigidbody2D?
Your custom solution likely works somewhat because your math has some issue, and duplicating the "push" it calculates solves it, but it's not an actual collision solver, and I'm surprised how well it currently works, considering that.
It would work better if you at least used a simple BoxCast2D in the direction of your velocity, and stopped the movement at the BoxCast2D distance if it hits.
Or just use a rigidbody2d.
Your answer
Follow this Question
Related Questions
Character hits invisible ghost collision when jumping against objects while pushing against them 0 Answers
destructible tilemap, destroying tiles through overlapcircle 1 Answer
How can i fix the collider of this one gradient Tile? 0 Answers
How to increase the size of Tilemap Collider 2D in Unity? 1 Answer
How can I implement box collider or mesh collider in this code? 0 Answers