Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to set/edit your physics step in code?

Discussion in 'Physics for ECS' started by Aleksander_Wit, Aug 6, 2023.

  1. Aleksander_Wit

    Aleksander_Wit

    Joined:
    Dec 13, 2020
    Posts:
    18
    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?
     
  2. bl4cksun

    bl4cksun

    Joined:
    Aug 15, 2015
    Posts:
    6
    You should be able to access the resulting PhysicsStep component at runtime in your system's OnUpdate() method the following way:

    Code (CSharp):
    1. if (SystemAPI.HasSingleton<PhysicsStep>()) {
    2.     var physicsStep = SystemAPI.GetSingletonRW<PhysicsStep>();
    3.     physicsStep.ValueRW.Gravity = new float3(0f, -5f, 0f);
    4. }
    Haven't tested this, but it should work that way.
     
    daniel-holz likes this.
  3. Aleksander_Wit

    Aleksander_Wit

    Joined:
    Dec 13, 2020
    Posts:
    18
    Would it be required to in the first place make a physics step or does the authoring component merely overwrite the default one?
     
  4. bl4cksun

    bl4cksun

    Joined:
    Aug 15, 2015
    Posts:
    6
    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.
     
  5. Aleksander_Wit

    Aleksander_Wit

    Joined:
    Dec 13, 2020
    Posts:
    18
    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.