- Home /
Editor Bug: GetContacts() lags 1 physics step behind, always
The following script illustrates the problem.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UnityBug : MonoBehaviour
{
ContactPoint2D[] contacts = new ContactPoint2D[20];
bool buffer_input = false;
void Start()
{
StartCoroutine(after_physics_step());
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space)) buffer_input = true;
}
void FixedUpdate()
{
if (buffer_input)
{
GetComponent<Rigidbody2D>().MovePosition(transform.position + Vector3.up);
}
}
IEnumerator after_physics_step()
{
while (true)
{
yield return new WaitForFixedUpdate();
if (GetComponent<Rigidbody2D>().GetContacts(contacts) > 0)
{
if (buffer_input) Debug.Log("contact points are lagging");
else Debug.Log("touching");
}
else
{
Debug.Log("airborne");
}
buffer_input = false;
}
}
}
In a scene like the one in the attached image, I place this script on the white square and align it flush with a box collider 2D. Pressing space moves the white square off the box collider.
The frame that the white square moves, it runs fixedupdate, which calls MovePosition, then does a physics step, then does GetContacts. GetContacts returns contact points with the floor, even though there is a one unit gap between the white square and the floor after the physics step.
If you don't like WaitForFixedUpdate you can manually step physics using Physics2D.Simulate and then read contact points. You will get the same issue. This happens in the most recent version of unity but has existed as an error since at least the 2019 version.
Edit: Here is a project file demoing the problem. It's just one scene and two objects with the script attatched. If you hit space and read the log, you will see "contact points are lagging" prints after you jump. link text
assu$$anonymous$$g you want the actual unity staff to take a look at this: They do not roam in this forum. If you think this is a bug you should use their bug report service and post this there.