RunnerSystem.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. using Unity.Burst;
  2. using Unity.Collections;
  3. using Unity.Entities;
  4. using Unity.Mathematics;
  5. using Unity.Transforms;
  6. namespace GPUECSAnimationBaker.Samples.SampleScenes.Marathon.RunnerSystems
  7. {
  8. [BurstCompile]
  9. public partial struct RunnerSystem : ISystem
  10. {
  11. [BurstCompile]
  12. public void OnUpdate(ref SystemState state)
  13. {
  14. state.Dependency = new RunnerJob()
  15. {
  16. deltaTime = SystemAPI.Time.DeltaTime,
  17. }.ScheduleParallel(state.Dependency);
  18. }
  19. [BurstCompile]
  20. private partial struct RunnerJob : IJobEntity
  21. {
  22. [ReadOnly] public float deltaTime;
  23. public void Execute(ref LocalTransform localTransform, in RunnerStateComponent runnerState)
  24. {
  25. float z = localTransform.Position.z;
  26. z += deltaTime * runnerState.speed;
  27. while (z > runnerState.fieldSizeZ / 2f) z -= runnerState.fieldSizeZ;
  28. localTransform.Position = new float3(localTransform.Position.x, 0f, z);
  29. }
  30. }
  31. }
  32. }