Physics debugger shows box colliders are different from scene view

So I’ve been at this all day. I’m instantiating several box colliders between frames for my sword collision physics. However it appeared to not be working. I just now enabled the Physics Debug window, and there appears to be inconsistencies between the positions that the Physics Debugger shows my box colliders and where the gizmo is drawn in the scene view.

You can see it in this video, I click on the physics box collider and it highlights the box collider on the game object. They’re in two different places. What could cause this?

This is the code that generates the box colliders:

            for(float x = 0f; x < 1; x += lerpStep)
            {
                GameObject temp = Instantiate(swordCollisionDetector, Vector3.Lerp(startTipPosition, endTipPosition, x), Quaternion.identity);
                temp.transform.LookAt(Vector3.Lerp(startBasePosition, endBasePosition, x), (swordTip.transform.position - temp.transform.position));
                //StartCoroutine(DestroySwordCollider(temp));
                swordColliders.Add(temp);
            }

This is running in LateUpdate so I can create the colliders after the animation updates the transforms.

It’s like the transform.LookAt isn’t being applied to the box collider…

Since I’m prototyping this, I’m working with Vector3s because I’m faster mentally with them vs Quaternions… Hence the transform.LookAt instead of setting the rotation in Instantiate.

For shits and giggles, I took a dive into Quaternions and worked out the rotation I need when I call Instantiate, instead of using LookAt. That solves my problem. The physics box colliders line up with the box collider gizmos.

I wanna say that anything modifying the orientation of a game object, other than Instantiate, won’t update the physics colliders until the following Internal Physics Update. However, I’m too lazy to set up a test for this, and I’ve got a solution to my issue in hand. :stuck_out_tongue:

Yes, modifying a transform doesn’t immediately apply the changes to colliders/rigidbodies. You need to either wait for the physics engine to undergo its next update cycle, or manually call Physics.SyncTransforms.

1 Like

Sweet, thanks for the info. :slight_smile:

I recently read the physics section in the manual and I don’t believe this was mentioned anywhere. I did a quick search for “SyncTransforms” and nothing came up. However, I just saw that you can enable “Auto Sync Transforms” in the physics settings, which I assume is similar to Physics.SyncTransforms, but just automatically ran any time a transform is modified. I would imagine that would cause unnecessary overhead though if you had lots of game objects.

With that on, any time you call a read operation on any physics object, sync-transforms is called prior to the operation and yes, this has a overhead. SyncTransforms is only there for legacy behavior prior to when the Transform system stopped producing notifications of changes to any internal Unity system (due to changes required for the Job System not wanting side-effects of Transform changes).

You should try avoid all situations where you need to use this.

1 Like