Infinity or NaN floating point numbers appear when calculating the transform matrix for a Collider

I see the following error occur when I make a Physics.Raycast call:

Infinity or NaN floating point numbers appear when calculating the transform matrix for a Collider. Scene hierarchy path "Units/Player1/BodyCollider"
UnityEngine.StackTraceUtility:ExtractStackTrace () (at C:/buildslave/unity/build/Runtime/Export/Scripting/StackTrace.cs:37)
UnityEngine.PhysicsScene:Internal_Raycast (UnityEngine.PhysicsScene,UnityEngine.Ray,single,UnityEngine.RaycastHit&,int,UnityEngine.QueryTriggerInteraction)
UnityEngine.PhysicsScene:Raycast (UnityEngine.Vector3,UnityEngine.Vector3,UnityEngine.RaycastHit&,single,int,UnityEngine.QueryTriggerInteraction) (at C:/buildslave/unity/build/Modules/Physics/ScriptBindings/Dynamics.bindings.cs:705)
UnityEngine.Physics:Raycast (UnityEngine.Vector3,UnityEngine.Vector3,UnityEngine.RaycastHit&,single,int) (at C:/buildslave/unity/build/Modules/Physics/ScriptBindings/Dynamics.bindings.cs:1279)

The specific raycast call I make is:

Physics.Raycast(particle.position, Vector3.down, out RaycastHit hit, 10, groundLayerMask)

Is this a bug with Unity's Physics.Raycast call? The stacktrace points exactly to this line in my code. I don't have reliable steps to reproduce this, and the "BodyCollider" that is mentioned is not on the same layer that the raycast is looking at.

I'm getting the same issue with OverlapBoxNonAlloc. The wild thing is that the scene heirarchy path it gives is to a collider that is excluded by the layermask!

You can get this for anything (not just physics, not just this call). If you’re passing in a nan value in a single, vector etc. Have you verified you’re not doing that?




Note again that the scene hierarchy path provided points to a collider that is excluded by the layerMask. I assume the fact that it's a trigger collider is tripping up the physics system somehow.

It's times like this that I lament how Unity remains the only major commercial game engine that doesn't make the source available, so I have no way to actually understand what's going on and why this error might be happening, much less contribute a fix. I am at the mercy of the Unity people, so help me God.

Note that I'm not in the 3D physics team but from what I know, the above isn't related to the query at all. I would guess that you have AutoSyncTransforms on which is bad for performance because what it does is check if any Transforms have changed (by the user) and if so, perform all the appropriate updates of Rigidbody/Collider before any read operations occurs such as reading a physics property or performing a query.

This is why, when given the message, you're saying the "path" isn't related to the query. It doesn't have to be related to the query. TBH you shouldn't be modifying the Transform really and/or not using AutoSyncTransforms.

As to what the message is saying, it's indicating that a Transform has gone bad. This can occur if something like a Joint simulation has "exploded" and caused NaNs to be written via the Rigidbody to the Transform or other reasons.

If you get this message, it's indicating the specific thing that had the problem so you could inspect that. You'd get an identical thing if you were to manually call "Physics.SyncTransforms".

More detail is that when a SyncTransform occurs on a GameObject with a Collider, the collider has to calculate a Matrix that contains the relative transformation from the Rigidbody to which it's attached. It's here that the calculation has gone wrong.

This doesn't exist anymore in 2023.1 but FYI:

bool Collider::GetRelativePositionAndRotation(const Transform& parent, Matrix4x4f& matrix)
    Matrix4x4f childMatrix;
    GetPositionAndRotation(GetComponent<Transform>(), childMatrix);
    Matrix4x4f parentMatrix = parent.GetWorldToLocalMatrixNoScale();
    MultiplyMatrices4x4(&parentMatrix, &childMatrix, &matrix);
    if (!IsFinite(matrix))
        TEMP_STRING msg = "Infinity or NaN floating point numbers appear when calculating the transform matrix for a Collider. "
            + GetSceneHierarchyPathDescriptive(this);
        ErrorStringObject(msg.c_str(), this);
        return false;
    return true;

Thank you very much for the detailed explanation of the error, and for the peek at the code!

You were totally correct that my problem was AutoSyncTransforms. This is an older project, first created in Unity 2017.1, so AutoSyncTransforms was enabled by default. I had no idea that this setting existed or that I was supposed to turn it off. Turning it off prevented the error from occurring and seems to have only broken a few minor things that will be easy to fix with manual calls to Physics.SyncTransforms.

I'm very curious, what is changing about physics in 2023.1 to delete that code?

Honestly, I have no idea of the details. The refactoring removed that, maybe there’s something similar to what’s in 2D where if a Transform has any kind of NaN then it’s reset to sensible values. The physics was simply telling you that a Transform is bad, not that there’s a bug in the physics engine. Often these kinds of warnings end up being removed because they are either no longer needed or that they are just loathed because of the console spam. No idea what’s the reason here.

1 Like