Search Unity

  1. Going to #UniteBerlin? Bring a buddy & get 25% off the 2nd ticket! More info here.
    Dismiss Notice
  2. Famed game designer and creator of The Sims, SimCIty, and Spore is looking for a 3D artist to join their team. Join the challenge.
    Dismiss Notice
  3. Unity 2017.4 has arrived! Read about it here.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. We've opened a new World Building sub-forum to faciltate all your world building discussions! Drop in and ask any ProBuilder, ProGrids, or PolyBrush questions.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

where/how set fixedDeltaTime independent of FixedUpdate()'s rate?

Discussion in 'Physics' started by Deeeds, Apr 16, 2018 at 3:08 PM.

  1. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    150
    Is it possible to set the fixedDeltaTime to a value independent of FixedUpdate's rate?

    If so, where and how?
     
  2. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    143
    FixedUpdate runs on the fixedTime, why do you have this need?
     
  3. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    150
    In the original post of this thread: https://forum.unity.com/threads/2d-...ion-simulation-groups-multiple-worlds.466501/

    ... there is discussion of manual simulation. I'm wanting to do that, but divorced from the FixedUpdate() rate, by means of a different fixedDeltaTime... I presume.

    Way down this thread, you'll see some dribble from me.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,311
    No. The FixedUpdate frequency is driven by Time.fixedDeltaTime.

    The documentation shows you how to run the simulation manually here and shows you an example of performing the simulation using Time.fixedDeltaTime updates. If you simply want to use the delta-frame-time then as I said in the thread you referred to then simply perform the following in the Update callback:
    Code (CSharp):
    1.     private void Update()
    2.     {
    3.         Physics2D.Simulate(Time.deltaTime);
    4.     }
    5.  
    You can add this to any GameObject in your scene. If you want you can also control the order this Update is called in relation to other scripts then you can use Script Exectution Order Settings.

    You should try to be more clear though. Saying you want to do manual simulation just means you want to do so in script. You don't specify if you want to do so in fixed-update or frame-delta so I just assumed you wanted frame-delta.
     
  5. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    150

    Doesn't this just change one dependency to another?

    In this, you're simply changing the frequency of the manual simulation to that of the Update() as opposed to the FixedUpdate().

    What if I want the manual simulation to occur at a faster rate than both of these things?
     
  6. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    143
    Sound like you want to set the fixed delta time to epsilon, haha
    You might wanna tell us what you're trying to achieve.
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,311
    I have no idea what that means. You have two choices for event functions in your scripts: FixedUpdate (executed at a fixed frequency of your choosing) or Update (executed each frame). These are not dependencies, they are your only choices.

    As I said, you've only said you want to do the simulation manually and I showed you how to do that but you're following up with odd statements. What is it that you want?

    That's a vague thing to ask TBH. You can step the simulation in whatever time-delta you wish either by manually doing this per-frame or using auto-simulation which runs in the fixed-update and change the fixed-update frequency to whatever you like.

    I agree. I've already told you the above several times now so unless you can provide a clear explanation of what it is you want then I'm not sure I can help you further even though I'd like to.
     
    Last edited: Apr 16, 2018 at 5:24 PM
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,311
    BTW: You mentioned you're fairly new to Unity so have you tried reading through the scripting overview? Look at execution order and event functions in particular.
     
  9. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    150
    @MelvMay

    I'm sorry if my understanding is failing...

    I want manual simulation to occur faster than Update() and faster than fixedUpdate().

    For the sake of discussion, let's assume I want manual simulation to occur 4x faster than either of these, and that they're both at 60fps.

    Can this be done?

    How?
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,311
    As I said here and in the other thread. Set the fixed-update to whatever you like in the TimeManager settings!!!!
     
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,311
    The thing is, if you set the fixed-update to 240Hz and you have your rendering locked to 60Hz then it cannot call the fixed-update at 240Hz as it'll only ever be called at 60Hz (the game engine is a big synchronous loop in the end) but the example loop you were saying doesn't work (in the other thread) would call it 4 times so it keeps up with game time.
     
  12. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    150
    Let me spell this out:

    I want FixedUpdate and Update to occur at 60fps... both of them.

    I want to manually simulate the physics at 240hz.

    Is this possible?

    And, yes, I realise that the Update is locked to 60Hz, that's the dependency I was talking about before... you simply swapped the dependency/reliance (choose your fav word here) of the manual simulation from the FixedUpdate to Update, in your previous example.

    I'm wanting manual simulation to be independent of both the Unity Updates, and to run at much faster than both of them.
     
  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,311
    There are only Update and FixedUpdate callbacks in Unity to perform periodic actions as described in the link I have you on execution orders and events. You are asking about manual simulation, for no apparent reason why you have to do it manually rather than let Unity do it at a fixed frequency you specify.

    Sorry but you just keep asking the same question which I have answered and I cannot think of another way of providing it. I've already repeated myself several times.
     
    Deeeds likes this.
  14. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    150
    Perhaps I missed it.

    Are you saying it is NOT possible to run manual simulation updates at 240hz whilst fixedUpdate and Update run at 60hz?
     
  15. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,311
  16. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    150
    I'd like manual simulation as I desire/need much faster simulation than I want/need FixedUpdate().

    For one thing, I'd like OnCollisionEnter and OnTriggerEnter to be operating at 240hz, whilst FixedUpdate() and Update operate at 120Hz or 60Hz. I want to push and exploit the newer iPad Pros for a very fast ball based game.
     
  17. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,311
    Why are you so set on the frequency of FixedUpdate and Update to be the same? What purpose would that serve? As I've said several times now, FixedUpdate is for performing a periodic update decoupled from the frame-rate but you seem to be ignoring that and artifically limiting it to be the same as Update.

    You do not need to perform manual simulation to control the update frequency of the physics.

    Unity does not provide some asynchronous callback at an arbitrary frequency. I can easily set-up physics to run in steps of 1/240 whilst the frame-rate is some other arbitrary value and do so as I've described a few times and do so without performing manual simulation.

    Maybe with a little experimentation of your own and following the info I've provided you can come to the same conclusion yourself. Beyond that, not sure what else I can provide to help you.
     
    Last edited: Apr 16, 2018 at 8:53 PM
  18. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    150

    Because:

    A. I'm doing a lot of stuff in FixedUpdate that's not necessary at faster than 60Hz, but is a bit of a drain when done at 240Hz... so I'd hoped to be able to set Simulation to be the only thing happening faster... where and when and upon what I need/desire it...

    and

    B. I got excited about the prospect of four things
    1) Layering of Physics
    2) Multiple Physics Worlds
    3) Being able to update some of the above and their elements VERY fast, very accurately,

    and...
    4) Mobile devices running on batteries having great physics, efficiently
     
  19. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,311
    So if FixedUpdate is always running at 60Hz and for some reason you have to simulate physics at 240Hz, that doesn't mean you have to physically call Simulate at 240Hz. In your FixedUpdate, simply call it four times with 1/240 as the delta-time.

    I've yet to come across a situation where running 2D physics at such a high frequency has any benefit beyond what increasing the solver iteration count does and indeed, so called accuracy doesn't increase much beyond a certain point. It's much more expensive to do this and 240Hz sounds like some arbitrary high processing rate.

    Also, you say you're doing stuff in FixedUpdate at 60Hz and also the same rate in Update but that simply means you can do the same stuff in Update and leave FixedUpdate for physics. It really sounds like you're putting some odd limitations on yourself here.

    Anyway, the above is the answer so I'll leave it there.
     
  20. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,311
    Running physics at 240Hz is an opposing force (no pun intended) to a mobile device running on batteries. You're adding extra CPU load by doing this.
     
    Deeeds likes this.
  21. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    150
    Please excuse me if I'm a moron... and there's every chance I am...

    I'm not sure how/what to put into the Delta-time "slot", as delta time is the time since the last frame, not something that I know of being able to set to 0.004166...

    So my question, what do I put where the ????? are in the below, and how do I call it four times? In a for loop?

    Code (CSharp):
    1. public class ManualPhysicsUpdate : MonoBehaviour {
    2.  
    3.     void Start ()
    4.     {  
    5.     Physics2D.autoSimulation = false;
    6.     }
    7.  
    8.     void FixedUpdate()
    9.     {
    10.     Physics2D.Simulate(?????);  
    11.     }  
    12. }
     
  22. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    150
    Yes, I know. This isn't all game, It's just for some extra special special effects, at a few crucial moments, and might be higher than 240Hz, may need 480Hz. Not sure... yet.
     
  23. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,311
    delta-time is simply an expression of how much time you want to simulate. That's got nothing to do with frame-delta-time in the TimeManager. Simply pass in 1/240 and call it four times.

    Well it's up to you but I very much doubt you'll see any benefit from running in such small time-deltas. 480Hz is a lot of of calls to simulate and will take up a significant amount of CPU depending on what's going on.

    Anyway, it's your project and only you know.
     
  24. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    150
    @MelvMay

    By way of example of how I perceive the problem... moronically:

    Which one of these is the right way to do this 240Hz manual simulation?


    PNG image.png
     

    Attached Files:

  25. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,311
    Assuming the first image means in parallel then you cannot run multiple simulation of the same physics scene in parallel because that doesn't even make sense. You have to run them synchronously in 1/240Hz steps.
     
    Deeeds likes this.
  26. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    150
    Thank you!!!

    How do I call them, at 1/240hz steps, from the FixedUpdate running at 60Hz?
     
  27. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,311
  28. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    150
  29. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,311
    Just pass 1.0f / 240.0f to the Physics2D.Simulate call!! Do that four times.

    Here's the code if you feel you cannot follow the above instructions for some reason.
    Code (CSharp):
    1. {
    2.     Physics2D.Simulate(1.0f / 240.0f);
    3.     Physics2D.Simulate(1.0f / 240.0f);
    4.     Physics2D.Simulate(1.0f / 240.0f);
    5.     Physics2D.Simulate(1.0f / 240.0f);
    6. }
     
  30. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,311
    I want to say again that it seems you're struggling with basic scripting and you're trying to do something that Unity will do for you out-of-the-box but you're completely ignoring that and saying you cannot use FixedUpdate to do this which you absolutely can. Using FixedUpdate and Update both at 60Hz is redundant if you're not using FixedUpdate for physics and the other for per-frame stuff. FixedUpdate comes before Update so just put your code in Update!!

    You can easily avoid all the bother you've gone through by simply changing the fixed-time-delta only. A few seconds to change.
     
  31. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    150
    I, quite seriously, have an enormous problem understanding time and maths in this context.

    I don't know how it is that the timing of this function can be made to call 4x more often than the FixedUpdate.

    That's a property of not knowing unity well enough, nearly no knowledge of C#, almost no understanding of how this fixedUpdate could possibly be made to call 4 separate times in exact time steps throughout one of its time steps, etc etc...

    if you think this is basic stuff... good for you.

    I promise not to make fun of you if you ever need help using design software.

    However...

    My problem remains.

    How is it that I call that simulate function 4x in one FixedUpdate and it simulates at equal spacings?

    A while loop or for loop that iterates four times over with simulate(0.004167) is going to fire them off one after another at CPU time, not at equal spacings of this time, right?

    How do they get queued?
     
  32. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,311
    I am not aiming to make fun of you in any way but to reiterate that you're making it hard for yourself.

    This is my problem and why I am telling you to go through some basic scripting tutorials. I explained how to do this in text and I gave you the code to do it yet you are asking me yet again.

    Nothing gets queued. They run sequentially and synchronously (look those terms up).
     
  33. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    150
    @MelvMay

    I must have missed this...

    I've been presuming the problem is this:

    a for loop or while loop

    while i < 4
    Simulate(0.0041667)
    i++


    I'm worried this is going to do the example at the top of this drawing... how do I prevent this?

    If it's automagically solved, by some kind of magical and invisible queuing inside the physics engine, how was I to know that?



    PNG image.png
     
  34. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,311
    Here it is:
    1. Start a new test project
    2. Go to TimeManager settings and change the fixed delta-time to 1/240Hz
    3. Press Play
    4. Physics is run in steps of 240Hz
    5. Add script code to Update which are called after physics has updated
    6. Job Done.
     
  35. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,311
  36. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    150
    "This is my problem and why I am telling you to go through some basic scripting tutorials."

    If there's any scripting tutorials that cover the internals of physics engines in Unity, I'm all about it!

    I want fixedUpdate at 60Hz, Simulate at 240Hz.

    This does NOT solve this problem. You push FixedUpdate to 240Hz... which is NOT what I want. How many times do I have to make this clear?
     
  37. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,311
    That's fine. You've been given all the information to achieve this but despite my absolute best efforts, unfortunately you don't seem to follow it. I've spent a lot of time trying to help you both here and in the other threads so I'm going to leave it at that sir because I am simply repeating myself over and over.

    I hope you spend a little time re-reading what has been posted and/or going through some basic scripting tutorials and find the help you need.
     
  38. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    150
    Perhaps the parallel communication problem is this:

    There may well be some kind of queuing system in Unity's Box2D physics that works automagically to sequence repeated calls to
    Simulate(timeUnit)
    regardless the Mach Absolute Time spacing between these calls.

    If you'd just come out and say something clear like:

    "Unity takes care of the spacing of
    Simulate(myTimeUnit)
    calls for you..."

    Then my concerns would be alleviated.

    I'm not sure how hard it is to see that's what I'm concerned about, combined with how to call Simulate at a rate faster than FixedUpdate and ensure it's evenly spaced at whatever desired frequency I have.
     
  39. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,311
    Why would I say that, it makes no sense at all. Yes, please tell me the words to use that you understand for problems that don't exist. What spacing? Huh? I fail to follow you.

    I said:
    • They are called sequentially and synchronously but you don't know what they mean presumably
    • They are not queued
    • I showed you the code to do it explicitly to which you've obviously not tried otherwise you'd see it working
    • I indicated in your first diagram that they don't run overlapped (this is sequential code execution, look it up) but one after the other to which you ended up posting the same image again asking me if it happened.
    You need to learn some fundamentals first.
     
  40. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,311
    Code (CSharp):
    1. void Update()
    2. {
    3.    Debug.Log("1");
    4.    Debug.Log("2");
    5.    Debug.Log("3");
    6.    Debug.Log("4");
    7. }
    Outputs 1, 2, 3 & 4 in the console. Sequential code execution. Doing the same for simulation or anything else means each one runs before the next one starts. There's no spacing or overlaps.
     
  41. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    150
    That's a bad example.

    A physics simulation, so far as I know, is a set of calculations attempting to determine (or simulate) what's going to be (as physics world state) in timeStep's time from the time of the call.

    That series of calculations, so far as I know, is rarely (if ever) going to take exactly timeStep to calculate. It will almost always be FAR faster than that, at which point a simulation state is returned. So far as I know, permitting the next Simulate(timeStep) to be called... if your example of Debug.Log is to be considered in this situation.

    In a manual Simulation environment, how do I know that Simulate(timeStep) is going to line them up sequentially based on timeStep's duration? Why wouldn't it just take a snapshot of where physics is at the time of the call, and calculate a simulation out timeStep from there?

    How am I to know that piling on Simulate(timeStep) is going to correctly line them up, spaced correctly, rather than base them on the timeStep's distance from the current Mach Absolute Time of the call to Simulate?

    I can't override Simulate(n) or use a Category to extend it so I can see when it completes and what it does. I have to use my eyes, at 240Hz, to test it... which is impossible.

    Hence... asking you!
     
  42. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,311
    I do not follow you at all. I hope someone else can help.
     
  43. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    150
    @MelvMay if the simulation is run faster than the FixedUpdate and Update functions, do Collision and Trigger events get called faster, too?
     
  44. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,311
    The question makes no sense really. You are getting very confused over the delta-time you're passing to the Simulate call and the frequency of the FixedUpdate/Update functions.

    When Physics2D.Simulate is called, before it returns, Collision/Trigger callbacks happen. The value passed to Physics2D.Simulate has no relevance.
     
    Last edited: Apr 18, 2018 at 1:43 PM
  45. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    150
    If I call Simulate at 240Hz, and FixedUpdate is 60Hz, as is Update, do Collision and Trigger callbacks happen at 240Hz?

    Simulate, I'm supposing, does enough simulating that it determines if there are any collisions and trigger events. I could be wrong about this.
     
  46. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,311
    You just asked the same question again without acknowledging my answer in any way. Hopefully someone else can help.
     
  47. user099

    user099

    Joined:
    May 29, 2015
    Posts:
    14
    i think he is asking about physics substeps integration such as Euler or Runge-Kutta 4 (RK4).
     
  48. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    150
    @MelvMay Serious question: do you have an aversion to the words "Yes" and "No"?

    And a passion for attempting to discredit my questions?

    Yes, seems to be the answer to both of these questions.

    These are yes and no questions.

    Do you think you could answer just ONE of my questions about simulate with a yes or no?

    @user099 I don't know what Euler or Runge-Kutta are, I'm simply trying to ascertain if Collision and Trigger callbacks will happen at the rate of manual simulation.

    I don't know why it's a question that can't be answered with a yes or no.
     
  49. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,311
    I am sorry you feel I'm trying to discredit you (which I'm not) and by saying the question makes no sense and clarifying what it does is not any attempt to do so. Please don't take me saying "no sense" to in any way be against you but with lack of understand on a subject sometimes comes questions that make no sense and do not have a yes or no answer.

    I admit to being slightly frustrated by not being able to help you at all. I thought my answer was clear and gave you the exact information but it seems to be too much information for you.

    I have given you a lot of my time trying to help. I am surprised you'd think I'd spend all that time simply trying to discourage and discredit you. Please try to read my text in a more friendly way.
     
    Deeeds likes this.
  50. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    150
    Tr

    Try, please, I'm begging you, try to answer this questions with just a yes or a no.... it will help me greatly!!!!

    If I call Simulate at 240Hz, and FixedUpdate is 60Hz, as is Update, do Collision and Trigger callbacks happen at 240Hz?