Randoms

A NativeArray<Unity.Mathematics.Random> on PhysicsStructuresComponent. Each slot is a persistent per-body random stream — one Random state per body index, parallel to BodiesList.

Internal systems use Randoms[index] for spatial-map work. You can read and write the same slots from custom systems when you need deterministic, per-body randomness without allocating your own RNG arrays.

Allocation and initialization

Bootstrap allocates Randoms with length MaxEntitiesCount, matching BodiesList.

Each entry is seeded from SpacialMapAuthoring.RandomSeed (baked into internal PhysicsMapRandomComponent, default 12345):

Randoms[i] = new Random(seed + (uint)i + 1u);

The per-index offset gives every body slot an independent stream even when ActiveBodiesCount is lower than the pool size.

Active body indices

Parallel jobs schedule over ActiveBodiesCount. Only indices 0 … ActiveBodiesCount - 1 participate in a given frame, but Randoms retains state for each index across frames.

Always write the updated Random back to Randoms[index] after drawing values so the stream stays consistent for internal systems and later custom jobs.

Custom job example

Schedule inside LittlePhysicsUserSystemGroup with IBodiesJob.IWriteIndex so you receive the body index for Randoms[index] while ScheduleAndChain handles BodiesList and PhysicsJobHandle. Pass Randoms as an extra field on the job struct.

using LittlePhysics;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;

[BurstCompile]
[UpdateInGroup(typeof(LittlePhysicsUserSystemGroup))]
public partial struct BodyRandomJitterSystem : ISystem
{
    [BurstCompile]
    public void OnCreate(ref SystemState state) =>
        state.RequireForUpdate<PhysicsReadyTag>();

    [BurstCompile]
    public void OnUpdate(ref SystemState state)
    {
        var structures = SystemAPI.GetSingleton<PhysicsStructuresComponent>();
        ref var simulation = ref SystemAPI.GetSingletonRW<SimulationDataComponent>().ValueRW;
        var deltaTime = SystemAPI.GetSingleton<LittlePhysicsTimeComponent>().DeltaTime;

        new BodyRandomJitterJob
        {
            Randoms = structures.Randoms,
            Strength = 0.5f * deltaTime,
        }.ScheduleAndChain(ref state, in structures, ref simulation);
    }

    [BurstCompile]
    private struct BodyRandomJitterJob : IBodiesJob.IWriteIndex
    {
        [NativeDisableParallelForRestriction]
        public NativeArray<Random> Randoms;

        public float Strength;

        public void Execute(in int index, ref PhysicsBodyData body)
        {
            var random = Randoms[index];

            if (!body.IsDynamic)
            {
                return;
            }

            float3 jitter = random.NextFloat3Direction() * Strength;
            var velocity = body.VelocityData;
            velocity.Linear += jitter;
            body.VelocityData = velocity;

            Randoms[index] = random;
        }
    }
}

Pair Randoms[index] with the body index from IWriteIndex when you need both random draws and body mutation in one pass. For raw IJobParallelFor when touching many buffers at once, see Custom job interfaces.


This site uses Just the Docs, a documentation theme for Jekyll.