forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCartesianGridMoveForwardSystem.cs
More file actions
42 lines (35 loc) · 1.7 KB
/
Copy pathCartesianGridMoveForwardSystem.cs
File metadata and controls
42 lines (35 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;
[UpdateBefore(typeof(TransformSystemGroup))]
public partial class CartesianGridMoveForwardSystem : SystemBase
{
protected override void OnUpdate()
{
// Clamp delta time so you can't overshoot.
var deltaTime = math.min(Time.DeltaTime, 0.05f);
// Move forward along direction in grid-space given speed.
// - This is the same for Plane or Cube and is the core of the movement code. Simply "move forward" along direction.
Entities
.WithName("CartesianGridMoveForward")
.ForEach((ref Translation translation,
in CartesianGridDirection gridDirection,
in CartesianGridSpeed gridSpeed,
in CartesianGridCoordinates gridPosition) =>
{
var dir = gridDirection.Value;
if (dir == 0xff)
return;
var pos = translation.Value;
// Speed adjusted to float m/s from fixed point 6:10 m/s
var speed = deltaTime * ((float)gridSpeed.Value) * (1.0f / 1024.0f);
// Write: add unit vector offset scaled by speed and deltaTime to current position
var dx = CartesianGridMovement.UnitMovement[(dir * 2) + 0] * speed;
var dz = CartesianGridMovement.UnitMovement[(dir * 2) + 1] * speed;
// Smooth y changes when transforming between cube faces.
var dy = math.min(speed, 1.0f - pos.y);
translation.Value = new float3(pos.x + dx, pos.y + dy, pos.z + dz);
}).Schedule();
}
}