3D_RPG_Action_Game/Player/Player2.cs

46 lines
1.3 KiB
C#

using Godot;
namespace DRPGActionGame.Player;
public partial class Player2 : CharacterBody3D
{
private const float SPEED = 5.0f;
private const float JUMP_VELOCITY = 4.5f;
// Get the gravity from the project settings to be synced with RigidBody nodes.
private float gravity = ProjectSettings.GetSetting("physics/3d/default_gravity").AsSingle();
[Export] private AnimationPlayer animationPlayer;
public override void _PhysicsProcess(double delta)
{
Vector3 velocity = Velocity;
// Add the gravity.
if (!IsOnFloor())
velocity.Y -= gravity * (float)delta;
// Handle Jump.
if (Input.IsActionJustPressed("ui_accept") && IsOnFloor())
velocity.Y = JUMP_VELOCITY;
// Get the input direction and handle the movement/deceleration.
// As good practice, you should replace UI actions with custom gameplay actions.
Vector2 inputDir = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down");
Vector3 direction = (Transform.Basis * new Vector3(inputDir.X, 0, inputDir.Y)).Normalized();
if (direction != Vector3.Zero)
{
animationPlayer.Play("Walking_A");
velocity.X = direction.X * SPEED;
velocity.Z = direction.Z * SPEED;
}
else {
animationPlayer.Play("Idle");
velocity.X = Mathf.MoveToward(Velocity.X, 0, SPEED);
velocity.Z = Mathf.MoveToward(Velocity.Z, 0, SPEED);
}
Velocity = velocity;
MoveAndSlide();
}
}