-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFPSCharacterController3D.cs
193 lines (155 loc) · 5.34 KB
/
FPSCharacterController3D.cs
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using Godot;
using System;
public partial class FPSCharacterController3D : CharacterBody3D
{
[Export] Node3D cam;
[ExportCategory("Physics")]
[Export] float jumpHeight = 5;
[Export] float jumpTime = 1.0f;
[Export] float currentSpeed = 5;
[Export] float walkingSpeed = 2.5f;
[Export] float sprintSpeed = 5.0f;
[Export] float accelerationSpeed = 100;
[Export] float decelerationSpeed = 15;
double gravity = 10;
double jumpForce = 5;
Vector3 rotation;
float pitch = 0;
float yaw = 0;
[ExportCategory("Input")]
[Export] Vector2 mouseDelta;
[Export] Vector2 turnDirection;
[Export] Vector2 moveDirection2D;
[Export] Vector3 moveDirection;
[Export] Vector3 horizontalVelocity;
[Export] Vector3 localVelocity;
[Export] double verticalVelocity;
[ExportCategory("Input Acceleration")]
[Export] float gamepadAcceleration = 2.5f;
[Export] float mouseSensitivity = 0.1f;
[Export] float mouseAcceleration = 1;
int jumpCount = 2;
int jumpCountMax = 2;
bool grounded;
bool jump;
bool sprint;
bool sprintKeyPressed;
// Called every frame. 'delta' is the elapsed lerpAmount since the previous frame.
public override void _PhysicsProcess(double delta)
{
gravity = PhysicsTools.DeriveGravity(jumpHeight, jumpTime / 2);
jumpForce = PhysicsTools.DeriveInitialVelocity(jumpHeight, jumpTime / 2, -gravity);
if (mouseDelta.LengthSquared() >= 0.1f)
{
yaw -= mouseDelta.X * mouseAcceleration;
pitch -= mouseDelta.Y * mouseAcceleration;
mouseDelta.X = 0;
mouseDelta.Y = 0;
}
if (turnDirection.Length() >= 0.1f)
{
yaw -= turnDirection.X * gamepadAcceleration;
pitch -= turnDirection.Y * gamepadAcceleration;
}
if ((grounded || jumpCount > 0) && jump)
{
jumpCount--;
verticalVelocity = jumpForce;
grounded = false;
jump = false;
}
if (sprintKeyPressed)
{
//sprint is true if the Key is pressed regardles of maxFlyingSpeed
sprint = true;
}
else if (!sprintKeyPressed && moveDirection.LengthSquared() < 0.1f)
{
//if we aren't holding the sprint Key AND we are not moving
sprint = false;
}
if (sprint)
{
currentSpeed = sprintSpeed;
}
else
{
currentSpeed = walkingSpeed;
}
yaw %= 360;
pitch = Mathf.Clamp(pitch, -60, 60);
this.Rotation = new Vector3(0, Mathf.DegToRad((float)yaw), 0);
cam.Rotation = new Vector3(Mathf.DegToRad((float)pitch), 0, 0);
horizontalVelocity = moveDirection.Rotated(Vector3.Up, Mathf.DegToRad((float)yaw)) * currentSpeed;
if (horizontalVelocity.LengthSquared() > 0.5f)
{
horizontalVelocity = horizontalVelocity.MoveToward(horizontalVelocity, (float)delta * accelerationSpeed);
}
else
{
horizontalVelocity = horizontalVelocity.MoveToward(horizontalVelocity, (float)delta * decelerationSpeed);
}
localVelocity.X = horizontalVelocity.X;
localVelocity.Z = horizontalVelocity.Z;
localVelocity.Y = (float)verticalVelocity;
verticalVelocity -= gravity * delta;
verticalVelocity = Mathf.Clamp(verticalVelocity, -gravity, 1000);
this.Velocity = localVelocity;
this.MoveAndSlide();
for (int i = 0; i < this.GetSlideCollisionCount(); i++)
{
var collision = this.GetSlideCollision(i);
}
if (verticalVelocity < 0 && this.IsOnFloor())
{
jumpCount = jumpCountMax;
verticalVelocity = 0;
grounded = true;
}
}
public override void _Input(InputEvent @event)
{
base._Input(@event);
if (@event is InputEventMouseButton eventMouseButton)
{
Input.MouseMode = Input.MouseModeEnum.Captured;
}
if (@event is InputEventMouseMotion eventMouseMotion)
{
mouseDelta += eventMouseMotion.Relative * mouseSensitivity;
}
if (@event.IsAction("TurnLeft") || @event.IsAction("TurnRight") || @event.IsAction("TurnDown") || @event.IsAction("TurnUp"))
{
turnDirection = Input.GetVector("TurnLeft", "TurnRight", "TurnDown", "TurnUp");
}
if (@event.IsActionPressed("Jump"))
{
jump = true;
}
if (@event.IsActionReleased("Jump"))
{
jump = false;
}
if (@event.IsActionPressed("Sprint"))
{
sprintKeyPressed = true;
}
if (@event.IsActionReleased("Sprint"))
{
sprintKeyPressed = false;
}
if (@event.IsAction("MoveLeft") || @event.IsAction("MoveRight") || @event.IsAction("MoveDown") || @event.IsAction("MoveUp"))
{
moveDirection2D = Input.GetVector("MoveLeft", "MoveRight", "MoveUp", "MoveDown");
moveDirection.X = moveDirection2D.X;
moveDirection.Z = moveDirection2D.Y;
}
if (@event is InputEventKey key)
{
if (key.Keycode is Key.Escape)
{
Input.MouseMode = Input.MouseModeEnum.Visible;
}
}
}
}