forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTankMovementSystem.cs
More file actions
42 lines (36 loc) · 1.37 KB
/
TankMovementSystem.cs
File metadata and controls
42 lines (36 loc) · 1.37 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.Burst;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
namespace Tutorials.Tanks.Step3
{
public partial struct TankMovementSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<Execute.TankMovement>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var dt = SystemAPI.Time.DeltaTime;
foreach (var (transform, entity) in
SystemAPI.Query<RefRW<LocalTransform>>()
.WithAll<Tank>()
.WithEntityAccess())
{
var pos = transform.ValueRO.Position;
// This does not modify the actual position of the tank, only the point at
// which we sample the 3D noise function. This way, every tank is using a
// different slice and will move along its own different random flow field.
pos.y = (float)entity.Index;
var angle = (0.5f + noise.cnoise(pos / 10f)) * 4.0f * math.PI;
var dir = float3.zero;
math.sincos(angle, out dir.x, out dir.z);
transform.ValueRW.Position += dir * dt * 5.0f;
transform.ValueRW.Rotation = quaternion.RotateY(angle);
}
}
}
}