-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathCharacterControllerPlayerInput.cs
More file actions
114 lines (109 loc) · 5.25 KB
/
CharacterControllerPlayerInput.cs
File metadata and controls
114 lines (109 loc) · 5.25 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.NetCode;
using Unity.NetCode.Samples.Common;
using UnityEngine;
namespace Samples.HelloNetcode
{
[GhostComponent(PrefabType=GhostPrefabType.AllPredicted, OwnerSendType = SendToOwnerType.SendToNonOwner)]
public struct CharacterControllerPlayerInput : IInputComponentData
{
[GhostField] public float2 Movement;
[GhostField] public InputEvent Jump;
[GhostField] public InputEvent PrimaryFire;
[GhostField] public InputEvent SecondaryFire;
[GhostField] public float Pitch;
[GhostField] public float Yaw;
/// <summary>Implemented to get better packet dump info.</summary>
public FixedString512Bytes ToFixedString() => $"move({Movement}, j:{Jump.Count}), shoot(p:{PrimaryFire.Count}, s{SecondaryFire.Count}), mouse(pitch:{Pitch}, yaw:{Yaw})";
}
[UpdateInGroup(typeof(HelloNetcodeInputSystemGroup))]
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
public partial struct SampleCharacterControllerPlayerInputSystem : ISystem
{
bool m_WasJumpTouch;
bool m_WasFireTouch;
bool m_WasSecondaryFireTouch;
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<CharacterControllerPlayerInput>();
state.RequireForUpdate<NetworkStreamInGame>();
}
public void OnUpdate(ref SystemState state)
{
var networkTime = SystemAPI.GetSingleton<NetworkTime>();
var netDebug = SystemAPI.GetSingleton<NetDebug>();
foreach (var input in SystemAPI.Query<RefRW<CharacterControllerPlayerInput>>().WithAll<GhostOwnerIsLocal>())
{
input.ValueRW.Movement = default;
input.ValueRW.Jump = default;
input.ValueRW.PrimaryFire = default;
input.ValueRW.SecondaryFire = default;
if (TouchInput.GetKey(TouchInput.KeyCode.LeftStick))
input.ValueRW.Movement = TouchInput.GetStick(TouchInput.StickCode.LeftStick);
else
{
if (Input.GetKey("left") || Input.GetKey("a"))
input.ValueRW.Movement.x -= 1;
if (Input.GetKey("right") || Input.GetKey("d"))
input.ValueRW.Movement.x += 1;
if (Input.GetKey("down") || Input.GetKey("s"))
input.ValueRW.Movement.y -= 1;
if (Input.GetKey("up") || Input.GetKey("w"))
input.ValueRW.Movement.y += 1;
}
var jumpTouch = TouchInput.GetKey(TouchInput.KeyCode.Space);
if (Input.GetKeyDown("space") || (jumpTouch && !m_WasJumpTouch))
input.ValueRW.Jump.Set();
m_WasJumpTouch = jumpTouch;
float2 lookDelta = float2.zero;
if (TouchInput.GetKey(TouchInput.KeyCode.RightStick))
{
lookDelta = TouchInput.GetStick(TouchInput.StickCode.RightStick) * SystemAPI.Time.DeltaTime;
}
#if !UNITY_IOS && !UNITY_ANDROID
else
{
lookDelta = new float2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
// You'll want to expose userSpecifiedMouseSensitivity in your games UI.
// The server doesn't need to know about it.
// Example valid range: 0.002 - 0.4.
const float userSpecifiedMouseSensitivity = .04f;
lookDelta *= userSpecifiedMouseSensitivity;
}
#endif
input.ValueRW.Pitch = math.clamp(input.ValueRW.Pitch+lookDelta.y, -math.PI/2, math.PI/2);
input.ValueRW.Yaw = math.fmod(input.ValueRW.Yaw + lookDelta.x, 2*math.PI);
var fireTouch = TouchInput.GetKey(TouchInput.KeyCode.Left);
var secondaryFireTouch = TouchInput.GetKey(TouchInput.KeyCode.Right);
if ((fireTouch && !m_WasFireTouch)
#if !UNITY_IOS && !UNITY_ANDROID
|| Input.GetKeyDown(KeyCode.Mouse0)
#endif
)
{
input.ValueRW.PrimaryFire.Set();
if (netDebug.LogLevel == NetDebug.LogLevelType.Debug)
{
netDebug.DebugLog($"[{state.WorldUnmanaged.Name}] PrimaryFire on tick:{networkTime.ServerTick.ToFixedString()}, fr:{Time.frameCount}!");
}
}
if ((secondaryFireTouch && !m_WasSecondaryFireTouch)
#if !UNITY_IOS && !UNITY_ANDROID
|| Input.GetKeyDown(KeyCode.Mouse1)
#endif
)
{
input.ValueRW.SecondaryFire.Set();
if (netDebug.LogLevel == NetDebug.LogLevelType.Debug)
{
netDebug.DebugLog($"[{state.WorldUnmanaged.Name}] SecondaryFire on tick:{networkTime.ServerTick.ToFixedString()}, fr:{Time.frameCount}!");
}
}
m_WasFireTouch = fireTouch;
m_WasSecondaryFireTouch = secondaryFireTouch;
}
}
}
}