Player movement
parent
bb94db3121
commit
6a9046849f
@ -0,0 +1,6 @@
|
||||
<Project Sdk="Godot.NET.Sdk/4.2.0-dev.5">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<EnableDynamicLoading>true</EnableDynamicLoading>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@ -0,0 +1,55 @@
|
||||
using Godot;
|
||||
|
||||
public partial class PlayerShip : Node3D {
|
||||
[Export] private float moveVelocity = 10f;
|
||||
|
||||
public override void _Ready() => Position = new(0, 0, 5);
|
||||
|
||||
public override void _Process(double delta) {
|
||||
HandleMovement(delta);
|
||||
}
|
||||
|
||||
private void HandleMovement(double delta) {
|
||||
Vector3 moveDir = new();
|
||||
Vector3 rotDir = new(0,180,0);
|
||||
|
||||
if (Input.IsKeyPressed(Key.A)) {
|
||||
moveDir.X -= moveVelocity * (float)delta;
|
||||
rotDir.Z = -30f;
|
||||
}
|
||||
|
||||
if (Input.IsKeyPressed(Key.D)) {
|
||||
moveDir.X += moveVelocity * (float)delta;
|
||||
rotDir.Z = 30f;
|
||||
}
|
||||
|
||||
if (Input.IsKeyPressed(Key.W)) {
|
||||
moveDir.Z -= moveVelocity * (float)delta;
|
||||
rotDir.X = -15f;
|
||||
}
|
||||
|
||||
if (Input.IsKeyPressed(Key.S)) {
|
||||
moveDir.Z += moveVelocity * (float)delta;
|
||||
rotDir.X = 15f;
|
||||
}
|
||||
|
||||
Position += moveDir;
|
||||
RotationDegrees = rotDir;
|
||||
CheckBoundaries();
|
||||
}
|
||||
|
||||
private void CheckBoundaries() {
|
||||
Vector3 correctedPos = Position;
|
||||
correctedPos.X = Position.X switch {
|
||||
< -3.3f => -3.3f,
|
||||
> 3.3f => 3.3f,
|
||||
_ => correctedPos.X
|
||||
};
|
||||
correctedPos.Z = Position.Z switch {
|
||||
< -5.2f => -5.2f,
|
||||
> 5.2f => 5.2f,
|
||||
_ => correctedPos.Z
|
||||
};
|
||||
Position = correctedPos;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue