forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveSystem.cs
More file actions
45 lines (38 loc) · 1.08 KB
/
Copy pathMoveSystem.cs
File metadata and controls
45 lines (38 loc) · 1.08 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
43
44
45
using Unity.Burst;
using Unity.Entities;
using Unity.Transforms;
namespace HelloCube.CrossQuery
{
public partial struct MoveSystem : ISystem
{
public float moveTimer;
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<ExecuteCrossQuery>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var dt = SystemAPI.Time.DeltaTime;
moveTimer += dt;
// periodically reverse direction and reset timer
bool flip = false;
if (moveTimer > 3.0f)
{
moveTimer = 0;
flip = true;
}
foreach (var (transform, velocity) in
SystemAPI.Query<RefRW<LocalTransform>, RefRW<Velocity>>())
{
if (flip)
{
velocity.ValueRW.Value *= -1;
}
// move
transform.ValueRW.Position += velocity.ValueRO.Value * dt;
}
}
}
}