forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacterOrientationSystem.cs
More file actions
47 lines (41 loc) · 1.68 KB
/
Copy pathCharacterOrientationSystem.cs
File metadata and controls
47 lines (41 loc) · 1.68 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
46
47
using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
namespace Samples.HelloNetcode
{
[BurstCompile]
[UpdateInGroup(typeof(HelloNetcodeSystemGroup))]
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation|WorldSystemFilterFlags.ServerSimulation)]
public partial struct CharacterOrientationSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<Character>();
state.RequireForUpdate<EnableCharacterOrientation>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
state.CompleteDependency();
foreach (var character in SystemAPI.Query<CharacterAspect>().WithAll<Simulate>())
{
if (!character.AutoCommandTarget.Enabled)
{
return;
}
var controllerConfig = SystemAPI.GetComponent<CharacterControllerConfig>(character.Character.ControllerConfig);
float2 input = character.Input.Movement;
float3 wantedMove = new float3(input.x, 0, input.y) * controllerConfig.Speed * SystemAPI.Time.DeltaTime;
// Wanted movement is relative to camera
wantedMove = math.rotate(quaternion.RotateY(character.Input.Yaw), wantedMove);
// Character orientation; turn towards movement
if (math.length(wantedMove) > 0)
{
quaternion forw = quaternion.LookRotation(math.normalize(wantedMove), math.up());
character.Transform.ValueRW.Rotation = forw;
}
}
}
}
}