Skip to content

Commit 9ae5c20

Browse files
authored
Refactor to get rid of CharacterController (#28)
* Get rid of CharacterController * Update .gitignore * Some cleanup
1 parent 30901c5 commit 9ae5c20

File tree

7 files changed

+33
-100
lines changed

7 files changed

+33
-100
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,6 @@ Assets/Plugins/polyperfect/
6767

6868
# Local user settings.
6969
UserSettings/
70+
71+
# Test builds.
72+
test_build*

Assets/Scenes/Game.unity

+1-21
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ GameObject:
211211
serializedVersion: 6
212212
m_Component:
213213
- component: {fileID: 366215642}
214-
- component: {fileID: 366215645}
215214
- component: {fileID: 366215644}
216215
m_Layer: 0
217216
m_Name: Player
@@ -253,28 +252,9 @@ MonoBehaviour:
253252
PlayerRunSpeed: 10
254253
JumpHeight: 1
255254
GravityValue: 9.81
256-
RotationSpeed: 30
257-
characterController: {fileID: 366215645}
255+
RotationSpeed: 15
258256
playerShoulderTarget: {fileID: 996116154}
259257
gameInput: {fileID: 297956314}
260-
--- !u!143 &366215645
261-
CharacterController:
262-
m_ObjectHideFlags: 0
263-
m_CorrespondingSourceObject: {fileID: 0}
264-
m_PrefabInstance: {fileID: 0}
265-
m_PrefabAsset: {fileID: 0}
266-
m_GameObject: {fileID: 366215641}
267-
m_Material: {fileID: 0}
268-
m_IsTrigger: 0
269-
m_Enabled: 1
270-
serializedVersion: 2
271-
m_Height: 2
272-
m_Radius: 0.5
273-
m_SlopeLimit: 45
274-
m_StepOffset: 0.3
275-
m_SkinWidth: 0.0001
276-
m_MinMoveDistance: 0.001
277-
m_Center: {x: 0, y: 1, z: 0}
278258
--- !u!1 &427920523
279259
GameObject:
280260
m_ObjectHideFlags: 0

Assets/Scripts/GameInput.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using UnityEngine;
32

43
public class GameInput : MonoBehaviour
@@ -13,9 +12,10 @@ private void Awake()
1312
playerInputActions.Player.Enable();
1413
}
1514

16-
public Vector2 GetMovementNormalized()
15+
public Vector3 GetMovement()
1716
{
18-
return playerInputActions.Player.Move.ReadValue<Vector2>().normalized;
17+
var move = playerInputActions.Player.Move.ReadValue<Vector2>();
18+
return Vector3.ClampMagnitude(new Vector3(move.x, 0f, move.y), 1f);
1919
}
2020

2121
public Vector2 GetLookAround()

Assets/Scripts/PlayerController.cs

+7-73
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
using System;
22
using UnityEngine;
33

4-
/// <summary>
5-
/// Enables a GameObject to move with WASD and jump with Space.
6-
/// </summary>
7-
[RequireComponent(typeof(CharacterController))]
84
public class PlayerController : MonoBehaviour
95
{
106
[Serializable]
117
struct Configuration
128
{
139
public float PlayerWalkSpeed;
10+
1411
public float PlayerRunSpeed;
1512

1613
/// <summary>
@@ -29,30 +26,6 @@ struct Configuration
2926
public float RotationSpeed;
3027
}
3128

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-
5629
[SerializeField]
5730
Configuration configuration = new()
5831
{
@@ -63,10 +36,6 @@ struct CurrentInput
6336
RotationSpeed = 3.0f,
6437
};
6538

66-
[SerializeField]
67-
[NotNull]
68-
CharacterController characterController;
69-
7039
[SerializeField]
7140
[NotNull]
7241
Transform playerShoulderTarget;
@@ -75,58 +44,26 @@ struct CurrentInput
7544
[NotNull]
7645
GameInput gameInput;
7746

78-
CurrentInput currentInput;
79-
float verticalSpeed;
80-
8147
public float HorizontalSpeed
8248
{
8349
get;
8450
private set;
8551
}
8652

8753
/// <summary>
88-
/// Used to compute currentSpeed. Do not use otherwise.
54+
/// Used to compute HorizontalSpeed. Do not use otherwise.
8955
/// </summary>
9056
float horizontalSpeedDampingValue;
9157

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-
12158
private float TargetSpeed
12259
{
12360
get
12461
{
125-
var isIdle = currentInput.Move == Vector3.zero;
62+
var isIdle = gameInput.GetMovement() == Vector3.zero;
12663
if (isIdle)
12764
return 0f;
12865

129-
if (currentInput.Run)
66+
if (gameInput.GetRun())
13067
return configuration.PlayerRunSpeed;
13168

13269
return configuration.PlayerWalkSpeed;
@@ -135,25 +72,22 @@ private float TargetSpeed
13572

13673
private void Update()
13774
{
138-
currentInput = GetCurrentInput(characterController, gameInput);
139-
verticalSpeed = UpdateVerticalVelocity(verticalSpeed, currentInput, configuration);
14075
HorizontalSpeed = Mathf.SmoothDamp(HorizontalSpeed, TargetSpeed, ref horizontalSpeedDampingValue, .3f);
14176

14277
var yRotation = Quaternion.Euler(0f, playerShoulderTarget.eulerAngles.y, 0f);
143-
var movementDirection = yRotation * currentInput.Move;
78+
var movementDirection = yRotation * gameInput.GetMovement();
14479
FaceMovementDirection(movementDirection);
14580
Move(movementDirection);
14681
}
14782

14883
private void Move(Vector3 movementDirection)
14984
{
150-
characterController.Move(currentInput.DeltaTime * HorizontalSpeed * movementDirection);
151-
characterController.Move(currentInput.DeltaTime * new Vector3(0f, verticalSpeed, 0f));
85+
transform.position += Time.smoothDeltaTime * HorizontalSpeed * movementDirection;
15286
}
15387

15488
private void FaceMovementDirection(Vector3 movementDirection)
15589
{
156-
if (currentInput.Move != Vector3.zero)
90+
if (gameInput.GetMovement() != Vector3.zero)
15791
{
15892
float singleStep = configuration.RotationSpeed * Time.smoothDeltaTime;
15993
transform.forward = Vector3.RotateTowards(transform.forward, movementDirection.normalized, singleStep, 0f);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"MonoBehaviour": {
3+
"Version": 4,
4+
"EnableBurstCompilation": true,
5+
"EnableOptimisations": true,
6+
"EnableSafetyChecks": false,
7+
"EnableDebugInAllBuilds": false,
8+
"EnableArmv9SecurityFeatures": false,
9+
"CpuMinTargetX32": 0,
10+
"CpuMaxTargetX32": 0,
11+
"CpuMinTargetX64": 0,
12+
"CpuMaxTargetX64": 0,
13+
"CpuTargetsX64": 72,
14+
"OptimizeFor": 0
15+
}
16+
}
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"MonoBehaviour": {
3-
"Version": 3,
3+
"Version": 4,
44
"DisabledWarnings": ""
55
}
66
}

ProjectSettings/EditorBuildSettings.asset

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ EditorBuildSettings:
66
serializedVersion: 2
77
m_Scenes:
88
- enabled: 1
9-
path: Assets/Scenes/URPSampleScene.unity
10-
guid: 99c9720ab356a0642a771bea13969a05
9+
path: Assets/Scenes/Game.unity
10+
guid: 3579100dc72eb420496990c1eb7e11a0
1111
m_configObjects: {}

0 commit comments

Comments
 (0)