1 year ago

#23869

test-img

Jonathan Schneider

Unity DOTS Cubes don't want to move properly

Hello i am currently learning DOTS but encountered a problem. I want to move some cubes but they don't want to. My System Script looks like this :

protected override JobHandle OnUpdate(JobHandle inputDeps)
{
    var deltaTime = Time.DeltaTime;
    
    return Entities.WithName("Cube").ForEach((ref Translation position, in Scale scale) =>
    {
            position.Value = NoisePosition(position.Value, deltaTime, scale.Value);
    }).Schedule(inputDeps);
}

The noise position function calculates a new position dependant on the currently given one. The cubes move only once, then they stay at that position and won't move further. So i guess the system sets the cube position but does not change the initial value. So i start in state1, calculate state2 from it, the cubes get moved to state2 but in the next frame it calculates with state1 again. An example picture is linked here : https://ibb.co/kGqb3pP The initial y position of the cubes is always zero, so the calculation seems to work.

I create the cubes with these lines :

var entity = entityManager.CreateEntity(cubeArchetype);
entityManager.SetComponentData(entity, new LocalToWorld {
    Value = new float4x4(quaternion.identity, 
    new Vector3(x * scale, 0, y * scale)) 
});
        
entityManager.AddComponentData(entity, new Translation() {
    Value = new float3(x * scale, 0, y * scale)});
entityManager.AddComponentData(entity, new Scale() { Value = scale });
entityManager.AddComponentData(entity, new WorldRenderBounds());
entityManager.AddSharedComponentData(entity, cubeRenderMesh);
entityManager.SetName(entity, "Cube"); 

To the movement code : I use the same function for a comparison approach with the standalone jobsystem. Since it works there the logic should be right:

Vector3 NoisePosition(Vector3 position, float time, float scale)
{ 
    float Noise(float x, float y) 
    { 
        var pos = math.float2(x, y); 
        return noise.snoise(pos); 
    }
    
    var noiseValue = Noise(
        position.x * (RandomHeigtParameters.waveSize * scale) +         
        RandomHeigtParameters.offsetSpeed * time,
        position.z * (RandomHeigtParameters.waveSize * scale) +
        RandomHeigtParameters.offsetSpeed * time);
    return new Vector3(position.x, noiseValue * 
        RandomHeigtParameters.height + RandomHeigtParameters.waveSize,
        position.z);
}

But if i replace the function call to a simple position.Value += deltaTime the cubes move correctly.

So to wrap it up, i am a bit confused and would apprechiate if anyone here knows enough about DOTS to help me out. Thanks in advance.

c#

unity-game-engine

unity-dots

0 Answers

Your Answer

Accepted video resources