-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacterControllerController.cs
More file actions
173 lines (131 loc) · 5.29 KB
/
Copy pathCharacterControllerController.cs
File metadata and controls
173 lines (131 loc) · 5.29 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2025 U.S. Federal Government (in countries where recognized)
// Copyright 2025 Dakota Crouchelli dakota.h.crouchelli.civ@us.navy.mil
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
namespace CREATIVE.Utility
{
/**
A script used to provide Move calls to a CharacterController based on
input from InputActions.
*/
[RequireComponent(typeof(CharacterController))]
public class CharacterControllerController : MonoBehaviour
{
[field: SerializeField] float GravityAcceleration = -9.81f;
float registeredGravityAcceleration;
[field: SerializeField] InputActionReference ForwardInputAction;
InputActionReference registeredForwardInputAction;
[field: SerializeField] InputActionReference BackwardInputAction;
InputActionReference registeredBackwardInputAction;
[field: SerializeField] InputActionReference LeftInputAction;
InputActionReference registeredLeftInputAction;
[field: SerializeField] InputActionReference RightInputAction;
InputActionReference registeredRightInputAction;
Dictionary<InputAction, float> currentInputValues;
bool forwardDominant = true;
bool leftDominant = true;
CharacterController registeredCharacterController;
bool registered = false;
void Start() => reRegister();
void OnEnable() => reRegister();
#if UNITY_EDITOR
void OnValidate() => reRegister();
#endif
void OnDisable() => unRegister();
void OnDestroy() => unRegister();
void reRegister()
{
unRegister();
registeredForwardInputAction = ForwardInputAction;
registeredBackwardInputAction = BackwardInputAction;
registeredLeftInputAction = LeftInputAction;
registeredRightInputAction = RightInputAction;
registeredGravityAcceleration = GravityAcceleration;
if
(
Application.isPlaying && isActiveAndEnabled &&
registeredForwardInputAction != null &&
registeredBackwardInputAction != null &&
registeredLeftInputAction != null &&
registeredRightInputAction != null
)
{
registeredForwardInputAction.action.started += processInput;
registeredForwardInputAction.action.canceled += processInput;
registeredBackwardInputAction.action.started += processInput;
registeredBackwardInputAction.action.canceled += processInput;
registeredLeftInputAction.action.started += processInput;
registeredLeftInputAction.action.canceled += processInput;
registeredRightInputAction.action.started += processInput;
registeredRightInputAction.action.canceled += processInput;
registeredCharacterController = GetComponent<CharacterController>();
currentInputValues = new Dictionary<InputAction, float>();
currentInputValues[registeredForwardInputAction.action] = 0f;
currentInputValues[registeredBackwardInputAction.action] = 0f;
currentInputValues[registeredLeftInputAction.action] = 0f;
currentInputValues[registeredRightInputAction.action] = 0f;
registered = true;
}
}
void unRegister()
{
if (registered)
{
registeredForwardInputAction.action.started -= processInput;
registeredForwardInputAction.action.canceled -= processInput;
registeredBackwardInputAction.action.started -= processInput;
registeredBackwardInputAction.action.canceled -= processInput;
registeredLeftInputAction.action.started -= processInput;
registeredLeftInputAction.action.canceled -= processInput;
registeredRightInputAction.action.started -= processInput;
registeredRightInputAction.action.canceled -= processInput;
registered = false;
}
}
void processInput(InputAction.CallbackContext context)
{
if (context.action == registeredForwardInputAction.action)
forwardDominant = (context.phase == InputActionPhase.Started);
if (context.action == registeredBackwardInputAction.action)
forwardDominant = (context.phase != InputActionPhase.Started);
if (context.action == registeredLeftInputAction.action)
leftDominant = (context.phase == InputActionPhase.Started);
if (context.action == registeredRightInputAction.action)
leftDominant = (context.phase != InputActionPhase.Started);
currentInputValues[context.action] = context.ReadValue<float>();
}
void Update()
{
if (Application.isPlaying && isActiveAndEnabled && registered)
{
float forwardSpeed, leftSpeed;
if (forwardDominant)
forwardSpeed = currentInputValues[registeredForwardInputAction.action];
else
forwardSpeed = -currentInputValues[registeredBackwardInputAction.action];
if (leftDominant)
leftSpeed = currentInputValues[registeredLeftInputAction.action];
else
leftSpeed = -currentInputValues[registeredRightInputAction.action];
registeredCharacterController.Move
(
(
(
(
(transform.forward * forwardSpeed).normalized +
(transform.right * -leftSpeed).normalized
).normalized *
MathF.Max(MathF.Abs(forwardSpeed), MathF.Abs(leftSpeed))
) +
// TODO: Actually use this variable as gravitational acceleration, instead of speed
transform.up * registeredGravityAcceleration
) *
Time.deltaTime
);
}
}
}
}