1
1
using System ;
2
2
using UnityEngine ;
3
3
4
- /// <summary>
5
- /// Enables a GameObject to move with WASD and jump with Space.
6
- /// </summary>
7
- [ RequireComponent ( typeof ( CharacterController ) ) ]
8
4
public class PlayerController : MonoBehaviour
9
5
{
10
6
[ Serializable ]
11
7
struct Configuration
12
8
{
13
9
public float PlayerWalkSpeed ;
10
+
14
11
public float PlayerRunSpeed ;
15
12
16
13
/// <summary>
@@ -29,30 +26,6 @@ struct Configuration
29
26
public float RotationSpeed ;
30
27
}
31
28
32
- [ Serializable ]
33
- struct CurrentInput
34
- {
35
- /// <summary>
36
- /// Current movement direction.
37
- /// </summary>
38
- public Vector3 Move ;
39
-
40
- /// <summary>
41
- /// Whether player is running.
42
- /// </summary>
43
- public bool Run ;
44
-
45
- /// <summary>
46
- /// Time elapsed since last frame.
47
- /// </summary>
48
- public float DeltaTime ;
49
-
50
- /// <summary>
51
- /// Whether player is grounded.
52
- /// </summary>
53
- public bool IsGrounded ;
54
- }
55
-
56
29
[ SerializeField ]
57
30
Configuration configuration = new ( )
58
31
{
@@ -63,10 +36,6 @@ struct CurrentInput
63
36
RotationSpeed = 3.0f ,
64
37
} ;
65
38
66
- [ SerializeField ]
67
- [ NotNull ]
68
- CharacterController characterController ;
69
-
70
39
[ SerializeField ]
71
40
[ NotNull ]
72
41
Transform playerShoulderTarget ;
@@ -75,58 +44,26 @@ struct CurrentInput
75
44
[ NotNull ]
76
45
GameInput gameInput ;
77
46
78
- CurrentInput currentInput ;
79
- float verticalSpeed ;
80
-
81
47
public float HorizontalSpeed
82
48
{
83
49
get ;
84
50
private set ;
85
51
}
86
52
87
53
/// <summary>
88
- /// Used to compute currentSpeed . Do not use otherwise.
54
+ /// Used to compute HorizontalSpeed . Do not use otherwise.
89
55
/// </summary>
90
56
float horizontalSpeedDampingValue ;
91
57
92
- static CurrentInput GetCurrentInput ( CharacterController controller , GameInput gameInput )
93
- {
94
- var movement = gameInput . GetMovementNormalized ( ) ;
95
- var input = new Vector3 ( movement . x , 0f , movement . y ) ;
96
- return new CurrentInput
97
- {
98
- Move = Vector3 . ClampMagnitude ( input , 1f ) ,
99
- Run = gameInput . GetRun ( ) ,
100
- DeltaTime = Time . smoothDeltaTime ,
101
- IsGrounded = controller . isGrounded ,
102
- } ;
103
- }
104
-
105
- static float UpdateVerticalVelocity ( float verticalVelocity , CurrentInput currentInput , Configuration config )
106
- {
107
- // Simulate force of gravity.
108
- verticalVelocity -= config . GravityValue * Time . deltaTime ;
109
-
110
- // However,
111
- if ( currentInput . IsGrounded && verticalVelocity < 0 )
112
- {
113
- // Then zero out y-component of velocity.
114
- // Must be slightly negative so that CharacterController's .isGrounded computation returns true.
115
- verticalVelocity = - .5f ;
116
- }
117
-
118
- return verticalVelocity ;
119
- }
120
-
121
58
private float TargetSpeed
122
59
{
123
60
get
124
61
{
125
- var isIdle = currentInput . Move == Vector3 . zero ;
62
+ var isIdle = gameInput . GetMovement ( ) == Vector3 . zero ;
126
63
if ( isIdle )
127
64
return 0f ;
128
65
129
- if ( currentInput . Run )
66
+ if ( gameInput . GetRun ( ) )
130
67
return configuration . PlayerRunSpeed ;
131
68
132
69
return configuration . PlayerWalkSpeed ;
@@ -135,25 +72,22 @@ private float TargetSpeed
135
72
136
73
private void Update ( )
137
74
{
138
- currentInput = GetCurrentInput ( characterController , gameInput ) ;
139
- verticalSpeed = UpdateVerticalVelocity ( verticalSpeed , currentInput , configuration ) ;
140
75
HorizontalSpeed = Mathf . SmoothDamp ( HorizontalSpeed , TargetSpeed , ref horizontalSpeedDampingValue , .3f ) ;
141
76
142
77
var yRotation = Quaternion . Euler ( 0f , playerShoulderTarget . eulerAngles . y , 0f ) ;
143
- var movementDirection = yRotation * currentInput . Move ;
78
+ var movementDirection = yRotation * gameInput . GetMovement ( ) ;
144
79
FaceMovementDirection ( movementDirection ) ;
145
80
Move ( movementDirection ) ;
146
81
}
147
82
148
83
private void Move ( Vector3 movementDirection )
149
84
{
150
- characterController . Move ( currentInput . DeltaTime * HorizontalSpeed * movementDirection ) ;
151
- characterController . Move ( currentInput . DeltaTime * new Vector3 ( 0f , verticalSpeed , 0f ) ) ;
85
+ transform . position += Time . smoothDeltaTime * HorizontalSpeed * movementDirection ;
152
86
}
153
87
154
88
private void FaceMovementDirection ( Vector3 movementDirection )
155
89
{
156
- if ( currentInput . Move != Vector3 . zero )
90
+ if ( gameInput . GetMovement ( ) != Vector3 . zero )
157
91
{
158
92
float singleStep = configuration . RotationSpeed * Time . smoothDeltaTime ;
159
93
transform . forward = Vector3 . RotateTowards ( transform . forward , movementDirection . normalized , singleStep , 0f ) ;
0 commit comments