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

OnFixedUpdate?

Discussion in 'Project Tiny' started by gear64Studios, Jan 13, 2019.

  1. gear64Studios

    gear64Studios

    Joined:
    Jan 13, 2019
    Posts:
    2
    I need update physics at fixed intervals to avoid frame dependence, what is the way to do it?
     
  2. Nkon

    Nkon

    Unity Technologies

    Joined:
    Jun 12, 2017
    Posts:
    65
    Hi there,

    Here's an example system that performs 50 steps per second:

    Code (JavaScript):
    1.  
    2. namespace game {
    3.     export class FixedUpdateSystem extends ut.ComponentSystem {
    4.         static accumulator = 0;
    5.         static timeStep = .02;
    6.         static stepCount = 0;
    7.         static logTime = 0;
    8.  
    9.         OnUpdate():void {
    10.             FixedUpdateSystem.accumulator += this.scheduler.deltaTime();
    11.             while (FixedUpdateSystem.accumulator >= FixedUpdateSystem.timeStep) {
    12.                 FixedUpdateSystem.accumulator -= FixedUpdateSystem.timeStep;
    13.                 FixedUpdateSystem.stepCount++;
    14.  
    15.                 // add your fixed timestep code here
    16.  
    17.             }
    18.  
    19.             // this will just log the stepCount every second, can be removed
    20.             FixedUpdateSystem.logTime += this.scheduler.deltaTime();
    21.             if (FixedUpdateSystem.logTime >= 1) {
    22.                 console.log(FixedUpdateSystem.stepCount);
    23.                 FixedUpdateSystem.logTime = FixedUpdateSystem.logTime % 1;
    24.             }
    25.         }
    26.     }
    27. }
    28.  
    Cheers!
     
    SugoiDev and gear64Studios like this.
  3. gear64Studios

    gear64Studios

    Joined:
    Jan 13, 2019
    Posts:
    2