How to set/edit your physics step in code?

My solution for having a gameobject in a subscene with a "PhysicsStepAuthoring" component did for a while break when I upgraded my project. Regardless of this, the SubScene often fails to load and I have to click reimport on it after many times I run the scene.

I have no need for subscenes in my project from what I am aware of yet. I would also like the player to be able to edit the physics step themselves from a settings menu at runtime. How would I change it from code?

You should be able to access the resulting PhysicsStep component at runtime in your system's OnUpdate() method the following way:

if (SystemAPI.HasSingleton<PhysicsStep>()) {
    var physicsStep = SystemAPI.GetSingletonRW<PhysicsStep>();
    physicsStep.ValueRW.Gravity = new float3(0f, -5f, 0f);
}

Haven't tested this, but it should work that way.

1 Like


Would it be required to in the first place make a physics step or does the authoring component merely overwrite the default one?

If I understood you correctly, you already have a game object with a PhysicsStepAuthoring component attached to it in your scene.

During the entity baking process, a PhysicsStep (runtime) component is automatically created from your authoring component. This PhysicsStep component is then accessible during runtime as described above. You don't have to manually create this component.


I would like to eliminate the one subscene I do have. This is the only thing I still needed it for. I would prefer to make the component manually in code.