While using Spine, I found a strange bug. Using this setup:
A bunch of “Parents”, which have a bunch of "Child"s with BoxCollider2Ds.
The parent has this script attached:
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class TestBug : MonoBehaviour
{
public void Update()
{
// Set the transform
this.transform.position = new Vector3(this.transform.position.x + Mathf.Epsilon, this.transform.position.y, this.transform.position.z);
this.transform.rotation = Quaternion.Euler( this.transform.rotation.eulerAngles.x, this.transform.rotation.eulerAngles.y, this.transform.eulerAngles.z );
this.transform.localScale = new Vector3(this.transform.localScale.x, this.transform.localScale.y, this.transform.localScale.z);
}
}
When selecting and then moving these, I get a massive amount of lag, dropping the editor to 10fps.
It seems like Unity is re-creating the collider every frame. This only happens in the editor.
In my main project where I found this bug, this bug happens whenever I pan the camera. However everything works beautifully whenever in Play mode.
Is there a workaround to this?
I’m using Unity 4.5.5
I updated to Unity 4.6.4 to see if it would fix the problem, but it’s still there.
Moving colliders relative to the rigid-body using Transforms or the collider offset can become very expensive as Box2D requires that the collider (fixtures in Box2D speak) need to be completely recreated. You should only ever ‘move’ colliders by moving the rigid-body they are attached to.
In your example above, the situation is even worse; you’re modifying the transform three times per frame. Any colliders on the GameObject where this script is added that had a rigid-body on a parent GameObject would be recreated three times. It may even be that you’re making it even worse by not even using a Rigidbody2D in which case you’re moving static colliders perhaps.
Know that 2D colliders in Box2D don’t have a nice transform between the rigid-body and the collider itself. The geometry lives in rigid-body space. This means that if you modify the transform that sits between the rigid-body and the collider geometry, that geometry has to be transformed again. Obviously it’s set-up to have the rigid-body itself move/rotation. Add to this that Box2D doesn’t let you simply modify geometry, it’s completely immutable once created so this means it needs to be recreated.
1 Like
Awesome, thank you very much. This popped up because Spine updates every frame in the editor, which wasn’t a problem until I added box colliders to the bones through Spines Bone Utility doodad.
To get around this, I just made sure that Bone Utility doesn’t update until I hit a button in the inspector.
Thanks again!
1 Like