Initial commit
This commit is contained in:
51
Scripts/Player.cs
Normal file
51
Scripts/Player.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using Godot;
|
||||
|
||||
public partial class Player : CharacterBody3D
|
||||
{
|
||||
[Export]
|
||||
private int _speed { get; set; } = 14;
|
||||
|
||||
[Signal]
|
||||
public delegate Vector3 MoveEventHandler();
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
Velocity = ProcessMovement();
|
||||
MoveAndSlide();
|
||||
EmitSignal(SignalName.Move, Position);
|
||||
}
|
||||
|
||||
private Vector3 ProcessMovement()
|
||||
{
|
||||
var direction = Vector3.Zero;
|
||||
var targetVelocity = Vector3.Zero;
|
||||
|
||||
if (Input.IsActionPressed("move_right"))
|
||||
{
|
||||
direction.X += 1.0f;
|
||||
}
|
||||
if (Input.IsActionPressed("move_left"))
|
||||
{
|
||||
direction.X -= 1.0f;
|
||||
}
|
||||
if (Input.IsActionPressed("move_back"))
|
||||
{
|
||||
direction.Z += 1.0f;
|
||||
}
|
||||
if (Input.IsActionPressed("move_forward"))
|
||||
{
|
||||
direction.Z -= 1.0f;
|
||||
}
|
||||
|
||||
if (direction != Vector3.Zero)
|
||||
{
|
||||
direction = direction.Normalized();
|
||||
GetNode<Node3D>("Pivot").LookAt(Position + direction, Vector3.Up);
|
||||
}
|
||||
|
||||
// Ground velocity
|
||||
targetVelocity.X = direction.X * _speed;
|
||||
targetVelocity.Z = direction.Z * _speed;
|
||||
return targetVelocity;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user