-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathCharacterControllerCameraSystem.cs
More file actions
38 lines (35 loc) · 1.56 KB
/
CharacterControllerCameraSystem.cs
File metadata and controls
38 lines (35 loc) · 1.56 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
using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using Unity.NetCode;
namespace Samples.HelloNetcode
{
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
[UpdateInGroup(typeof(PresentationSystemGroup))]
[BurstCompile]
partial struct CharacterControllerCameraSystem : ISystem
{
public static readonly float3 k_CameraOffset = new float3(0, 2, -5);
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<EnableCharacterController>();
state.RequireForUpdate<NetworkStreamInGame>();
state.RequireForUpdate<Character>();
}
public void OnUpdate(ref SystemState state)
{
var camera = UnityEngine.Camera.main;
//We need to access the LocalToWorld matrix to match the position of the player in term of presentation.
//Because Physics can be either Interpolated or Predicted, we the LocalToWorld can be different than the real world position
//of the entity.
foreach (var (localToWorld, input) in SystemAPI.Query<RefRO<LocalToWorld>, RefRO<CharacterControllerPlayerInput>>().WithAll<GhostOwnerIsLocal>())
{
camera.transform.rotation = math.mul(quaternion.RotateY(input.ValueRO.Yaw), quaternion.RotateX(-input.ValueRO.Pitch));
var offset = math.rotate(camera.transform.rotation, k_CameraOffset);
camera.transform.position = localToWorld.ValueRO.Position + offset;
}
}
}
}