Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Physics.Simulate

Discussion in '2017.1 Beta' started by Misscelan, Jun 9, 2017.

  1. Misscelan

    Misscelan

    Joined:
    Mar 8, 2013
    Posts:
    176
    Hello there,

    Today I was experimenting with this and I got a bit confused.
    I thought this would allow us to call the simulation whenever we would like and with a variable time step and have it fully sync with the normal update.

    I'm currently calling it like this:
    Code (CSharp):
    1. void Update () {
    2.         Physics.Simulate(Time.time - m_timer);
    3.         m_timer = Time.time;
    4.     }
    5. }
    And most of the times I get 2 calls to the FixedUpdates inside my scripts for each normal Update loop.
    Is that the expected behaviour? Is there a possibility of syncing this with the normal Update so the FixedUpdates are called only once?

    Thanks!
     
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
  3. Misscelan

    Misscelan

    Joined:
    Mar 8, 2013
    Posts:
    176
    yep, sorry for to mention that in my previous email. This is the Start of the same component of the update:

    Code (CSharp):
    1. void Start () {
    2.         Physics.autoSimulation = false;
    3.         m_timer = Time.time;
    4.  
    5.     }
    EDIT: The funny thing is that if I modify the Fixed TimeStep in the Project Setting it seems to modify the number of times FixedUpdate is called.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,481
    Physics2D.Simulate has nothing whatsoever to do with FixedUpdate callbacks as it says in the docs.

    FixedUpdate is just a callback, it isn't directly related to physics; it just gives you a callback with a fixed timestep. It'll still get called more than once if it needs to catch up. FixedUpdate will still happen even if you turn off the auto-simulation. It's just that physics won't be called during the fixed update and you are left to call it yourself either in the fixed update or per-frame as you're doing here.

    Doing it per-frame will be more expensive as you're potentially calling it much more often and you'll not get a consistent simulation as it's a variable time so calculations will be different each time depending on frame-rate. If that's not a problem then you can do this.

    Also, if you're doing it per-frame then don't use interpolation on Rigidbody as it's a waste as interpolation is not used.
     
    Last edited: Jun 9, 2017
    Peter77 and Misscelan like this.
  5. Misscelan

    Misscelan

    Joined:
    Mar 8, 2013
    Posts:
    176
    Thanks for the clarification, that explains things!
    I've always seen the FixedUpdate tighted to the Physics so I thought that would be still connected.

    So, if you don´t update Physics.Simulate at a Time.fixedDeltaTime rate, then FixedUpdate is a completely disconnected callback with no specific purpose, am I correct assuming that?
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,481
    It's more subtle than that. What you think of as FixedUpdate is simply the engine calling "MonoBehaviour.FixedUpdate" (scripts) each fixed interval. After those have been called, with "autoSimulate=true" then the engine implicitly simulates physics with the fixed time interval. If you set "autoSimulate=false" then the engine still calls "MonoBehaviour.FixedUpdate" but it doesn't simulate physics at all and leaves that to you. You are free to call it during the "MonoBehaviour.FixedUpdate" yourself with a fixed time interval or during "MonoBehaviour.Update" (per-frame) with the frame time interval.

    You can see this here as a diagram: https://docs.unity3d.com/Manual/ExecutionOrder.html