-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathPlayerMovementSystem.cs
68 lines (60 loc) · 2.71 KB
/
PlayerMovementSystem.cs
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using Tutorials.Kickball.Execute;
using Tutorials.Kickball.Step1;
using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;
namespace Tutorials.Kickball.Step2
{
public partial struct PlayerMovementSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<PlayerMovement>();
state.RequireForUpdate<Config>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var config = SystemAPI.GetSingleton<Config>();
// Get directional input. (Most of the UnityEngine.Input are Burst compatible, but not all are.
// If the OnUpdate, OnCreate, or OnDestroy methods needs to access managed objects or call methods
// that aren't Burst-compatible, the [BurstCompile] attribute can be omitted.
var horizontal = Input.GetAxis("Horizontal");
var vertical = Input.GetAxis("Vertical");
var input = new float3(horizontal, 0, vertical) * SystemAPI.Time.DeltaTime * config.PlayerSpeed;
// If there's no directional input this frame, we don't need to move the players.
if (input.Equals(float3.zero))
{
return;
}
var minDist = config.ObstacleRadius + 0.5f; // the player capsule radius is 0.5f
var minDistSQ = minDist * minDist;
// For every entity having a LocalTransform and Player component, a read-write reference to
// the LocalTransform is assigned to 'playerTransform'.
foreach (var playerTransform in
SystemAPI.Query<RefRW<LocalTransform>>()
.WithAll<Player>())
{
var newPos = playerTransform.ValueRO.Position + input;
// A foreach query nested inside another foreach query.
// For every entity having a LocalTransform and Obstacle component, a read-only reference to
// the LocalTransform is assigned to 'obstacleTransform'.
foreach (var obstacleTransform in
SystemAPI.Query<RefRO<LocalTransform>>()
.WithAll<Obstacle>())
{
// If the new position intersects the player with a wall, don't move the player.
if (math.distancesq(newPos, obstacleTransform.ValueRO.Position) <= minDistSQ)
{
newPos = playerTransform.ValueRO.Position;
break;
}
}
playerTransform.ValueRW.Position = newPos;
}
}
}
}