Well the reason I ask is that it’s the behavior I’m seeing. I agree it would be a bad design, but I’ve seen plenty of bad things, poorly-named functions, etc. before
You probably won’t like to hear this, but I generally do not use rigid bodies in 2D. I’m sure for good and proper physics simulations or games that are very physics-driven they’re great, but for the types of projects I work on, they simply do not offer the precision, control, or responsiveness that I need. No amount of tweaking of the body types or update frequency/type or anything like that has ever removed the corner cases that I experience with rigid bodies, and working 99.8% of the time isn’t good enough. To the extent that I do use them, it’s for eventing purposes only (OnCollisionEnter etc.), and I always freeze position and rotation. I’m sure this is deeply irritating to a physics engineer, but I guarantee you that I am far from the only person who’s needed to do this, and it’s not a decision I’ve arrived at lightly. Even in Unity’s Sample 2D projects you’ll see a litany of collider casts and such designed to work around the types of problems that I deal with frequently.
I do make liberal use of the 2D Colliders though–I use them for collider casts, ray casts, etc., and I find they provide me with very reliable results that I can use to drive my movement logic off of. That’s why I bring up autoSyncTransforms, because I do modify the transform directly. Generally my strategy is to set up some barriers for updating the collider transforms so that it doesn’t need to happen literally every time I move an object, but I will toggle autoSyncTransforms sometimes to eliminate that as a source of an issue.
I just took some time to reproduce this in a test scene. No rigidbodies, only transform, sprite renderer, BoxCollider2D, and this script. Tested in 2021.3.9f1 and 2022.1.19f1. Project file is attached. The ‘LogError’ statement is hitting quite frequently for me.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FallScript : MonoBehaviour
{
public float minV = 1;
public float maxV = 5;
private Vector3 _startPos;
private Vector2 _velocity;
private Collider2D[] _overlapBuffer = new Collider2D[5];
// Start is called before the first frame update
void Start()
{
Physics2D.autoSyncTransforms = true;
_startPos = transform.position;
_velocity = new Vector2(0, -Random.Range(minV, maxV));
}
// Update is called once per frame
void Update()
{
int numOverlaps = GetComponent<Collider2D>().OverlapCollider(new ContactFilter2D(), _overlapBuffer);
for (int i = 0; i < numOverlaps; i++)
{
Collider2D overlapCollider = _overlapBuffer[i];
if (!overlapCollider.bounds.Intersects(GetComponent<Collider2D>().bounds))
{
Debug.LogError($"At velocity {_velocity}, {gameObject.name} overlaps collider {overlapCollider.name} when bounds do not intersect. Y position: {transform.position.y}, other Y Position: {overlapCollider.transform.position.y}");
}
else
{
transform.position = _startPos;
}
}
transform.position += (Vector3)(_velocity * Time.deltaTime);
}
}
Every single collider in the scene has a size y component of 1, and these are the kinds of things I’m seeing in the error logs with the above statement:
At velocity (0.00, -3.19), FallSquare (2) overlaps collider Square when bounds do not intersect. Y position: -3.304479, other Y Position: -4.32
9063442–1253401–TestOverlapIssue.zip (50.7 KB)