Add jumping

This commit is contained in:
Jens Pitkänen 2024-08-18 18:28:11 +03:00
parent 326ba481f8
commit c7a4bf7e81
2 changed files with 34 additions and 12 deletions

View File

@ -1057,9 +1057,10 @@ size = Vector3(87.9724, 96.1174, 91.8458)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.0415039, 58.3319, 0.0198975) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.0415039, 58.3319, 0.0198975)
size = Vector3(43.3174, 26.5295, 43.4319) size = Vector3(43.3174, 26.5295, 43.4319)
[node name="Player" type="CharacterBody3D" parent="."] [node name="Player" type="CharacterBody3D" parent="." node_paths=PackedStringArray("Eye")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 4.51124, 0) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 4.51124, 0)
script = ExtResource("10_87t4r") script = ExtResource("10_87t4r")
Eye = NodePath("PlayerEyeCamera")
[node name="PlayerEyeCamera" type="Camera3D" parent="Player"] [node name="PlayerEyeCamera" type="Camera3D" parent="Player"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.7, 0) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.7, 0)

View File

@ -7,33 +7,48 @@ namespace Gmtk24 {
[Export] [Export]
public float SprintMultiplier = 1.5f; public float SprintMultiplier = 1.5f;
[Export] [Export]
public float JumpVelocity = 10;
[Export]
public float JumpBufferLengthSeconds = 0.2f;
[Export]
public Node3D Eye; public Node3D Eye;
private float CurrentYaw = 0;
private float CurrentPitch = 0; private float CurrentPitch = 0;
private float JumpBufferTime = 0;
public override void _Ready() { public override void _Ready() {
// Input.MouseMode = Input.MouseModeEnum.Captured; Input.MouseMode = Input.MouseModeEnum.Captured;
} }
public override void _Input(InputEvent @event) { public override void _UnhandledInput(InputEvent @event) {
if (@event is InputEventMouseMotion ev && Input.MouseMode == Input.MouseModeEnum.Captured) { if (@event is InputEventMouseMotion ev && Input.MouseMode == Input.MouseModeEnum.Captured) {
var cameraSensitivityX = 3f; // FIXME: use camera sensitivity and invert settings var cameraSensitivityX = 2f; // FIXME: use camera sensitivity and invert settings
var cameraSensitivityY = 3f; // FIXME: use camera sensitivity and invert settings var cameraSensitivityY = 2f; // FIXME: use camera sensitivity and invert settings
Eye.RotateY(ev.Relative.X * 0.01f * cameraSensitivityX); var mouseMultiplier = 0.0003f;
CurrentPitch += ev.Relative.Y * 0.01f * cameraSensitivityY; CurrentYaw -= ev.Relative.X * mouseMultiplier * cameraSensitivityX;
CurrentPitch -= ev.Relative.Y * mouseMultiplier * cameraSensitivityY;
GetViewport().SetInputAsHandled();
} }
} }
public override void _Process(double delta) { public override void _Process(double delta) {
var yawInput = Input.GetActionStrength("look_left") - Input.GetActionStrength("look_right"); var yawInput = Input.GetActionStrength("look_left") - Input.GetActionStrength("look_right");
var pitchInput = Input.GetActionStrength("look_up") - Input.GetActionStrength("look_down"); var pitchInput = Input.GetActionStrength("look_up") - Input.GetActionStrength("look_down");
var cameraSensitivityX = 3f; // FIXME: use camera sensitivity and invert settings var cameraSensitivityX = 2f; // FIXME: use camera sensitivity and invert settings
var cameraSensitivityY = 3f; // FIXME: use camera sensitivity and invert settings var cameraSensitivityY = 2f; // FIXME: use camera sensitivity and invert settings
Eye.RotateY(yawInput * cameraSensitivityX * (float)delta); CurrentYaw += yawInput * cameraSensitivityX * (float)delta;
if (Eye != null) { if (Eye != null) {
CurrentPitch += pitchInput * cameraSensitivityY * (float)delta; CurrentPitch += pitchInput * cameraSensitivityY * (float)delta;
CurrentPitch = Mathf.Clamp(CurrentPitch, -Mathf.Pi / 2, Mathf.Pi / 2); CurrentPitch = Mathf.Clamp(CurrentPitch, -Mathf.Pi * 0.49f, Mathf.Pi * 0.49f);
Eye.Quaternion = new Quaternion(Vector3.Right, CurrentPitch); }
Eye.Quaternion = new Quaternion(Vector3.Up, CurrentYaw) * new Quaternion(Vector3.Right, CurrentPitch);
if (JumpBufferTime > 0) {
JumpBufferTime -= (float)delta;
}
if (Input.IsActionJustPressed("jump")) {
JumpBufferTime = JumpBufferLengthSeconds;
} }
} }
@ -47,11 +62,17 @@ namespace Gmtk24 {
if (Input.IsActionPressed("sprint")) { if (Input.IsActionPressed("sprint")) {
move *= SprintMultiplier; move *= SprintMultiplier;
} }
Vector3 vel = Velocity; Vector3 vel = Velocity;
vel.X = move.X; vel.X = move.X;
vel.Z = move.Z; vel.Z = move.Z;
vel += GetGravity() * (float)delta; vel += GetGravity() * (float)delta;
if (IsOnFloor() && JumpBufferTime > 0) {
JumpBufferTime = 0;
vel += Vector3.Up * JumpVelocity;
}
Velocity = vel; Velocity = vel;
MoveAndSlide(); MoveAndSlide();
} }
} }