A FixedUpdate Equivalent in Unity DOTS

How to properly create physics-related systems!

Created on February 26, 2021.

By default, systems belong to the SimulationSystemGroup, which is not called with fixed frame-rate frequency. Nested within it is another group called the FixedStepSimulationSystemGroup. As its name implies, the latter group is fixed, making it ideal for physics-related processing. Thus, here's a code snippet for creating physics-related systems at a fixed step:

using Unity.Entities;
using Unity.Jobs;
using Unity.Physics;
using Unity.Physics.Systems;
using Unity.Transforms;

[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
[UpdateBefore(typeof(BuildPhysicsWorld))]
public class SomePhysicalSystem : SystemBase
{
BuildPhysicsWorld buildPhysicsWorld => World.GetOrCreateSystem<BuildPhysicsWorld>();

protected override void OnUpdate()
{
Entities
.ForEach((Entity entity, ref PhysicsVelocity velocity, ref PhysicsMass mass) =>
{
// TODO
})
.WithName("SomePhysicalJob")
.ScheduleParallel();

buildPhysicsWorld.AddInputDependency(Dependency);
}
}

Note the use of the UpdateInGroup and UpdateBefore attributes to ensure the correct system update order.

There is also a need to add the current dependency as input to the BuildPhysicsWorld, because, otherwise, that built-in system may not wait for physics-related changes that could have been prompted by custom processing.

To stay informed on current best practices with Unity DOTS, I recommend watching Unity's official EntityComponentSystemSamples GitHub repository, which includes physics demos.

Next

The Best Indie Games of 2021

Previous

What Makes WaveFunctionCollapse Great


By using this site, you agree that you have read and understand its Privacy Policy.