best approach to Lock Angular Axes (X and Z) on Physics Entities

Hello Everyone,

im looking for a good way to Lock X and Z Angular Axes for some Dynamic bodies (Pure Enities). (Characters)
why does the Unity PhysicsBody do not support it like the Old Rogidbody ?

Thank you.

In the Physics Mass IComponentData, there’s a float3 field called InverseInertia. Set the X and Z components of that field to 0 to lock rotation around those axes.

1 Like

AFAIK Unity Physics doesn’t have an authoring component to set up rotation locking, so I made one myself:

[DisallowMultipleComponent, RequiresEntityConversion, RequireComponent(typeof(PhysicsBodyAuthoring))]
public class FreezeRotationAuthoring : MonoBehaviour
{
    public bool3 Flags;
}

[UpdateAfter(typeof(PhysicsBodyConversionSystem))]
public class FreezeRotationConversionSystem : GameObjectConversionSystem
{
    protected override void OnUpdate()
    {
        Entities.ForEach((FreezeRotationAuthoring freezeRotation) =>
        {
            Entity entity = GetPrimaryEntity(freezeRotation.gameObject);
            PhysicsMass mass = DstEntityManager.GetComponentData<PhysicsMass>(entity);
            if (freezeRotation.Flags.x)
                mass.InverseInertia.x = 0f;
            if (freezeRotation.Flags.y)
                mass.InverseInertia.y = 0f;
            if (freezeRotation.Flags.z)
                mass.InverseInertia.z = 0f;
            DstEntityManager.SetComponentData(entity, mass);
        });
    }
}
3 Likes

Thank you!

What does InverseInertia do? Is it 1 / Inertia?

@starikcetin Pretty much. It’s common for physics engines to opt for inverse values for mass and inertia for performance.

2 Likes

Upon further inspection, this is not the case. If you check the “Override Default Mass Distribution” toggle in PhysicsBodyAuthoring, a float3 field called “InertiaTensor” will be visible. You can lock rotation around an axis by setting the corresponding component of that field to Infinity.

5 Likes