Files
MovementTests/scenes/components/movement/CGroundedMovement.cs
2026-05-15 15:29:24 +02:00

37 lines
1.4 KiB
C#

using Godot;
using Movementtests.interfaces;
namespace Movementtests.scenes.movement;
[GlobalClass, Icon("res://assets/ui/IconGodotNode/node_3D/icon_path_follow.png")]
public partial class CGroundedMovement : Node3D, IMoveable
{
[Export] public RMovement RMovement { get; set; } = null!;
[Export] public RayCast3D WallInFrontRayCast { get; set; } = null!;
public Vector3 ComputeVelocity(MovementInputs inputs)
{
var velocity = inputs.Velocity;
var target = inputs.TargetLocation;
var direction = (target - GlobalPosition).Normalized();
var targetPlane = new Vector3(target.X, GlobalPosition.Y, target.Z);
LookAt(targetPlane);
var targetXSpeed = inputs.IsStunned ? 0 : direction.X * RMovement.Speed;
var targetZSpeed = inputs.IsStunned ? 0 : direction.Z * RMovement.Speed;
float xAcc = (float) Mathf.Lerp(velocity.X, targetXSpeed, inputs.Delta * RMovement.Acceleration);
float zAcc = (float) Mathf.Lerp(velocity.Z, targetZSpeed, inputs.Delta * RMovement.Acceleration);
velocity.X = xAcc;
velocity.Z = zAcc;
if (WallInFrontRayCast.IsColliding() && !inputs.IsStunned)
velocity.Y = RMovement.Speed;
else if (!inputs.IsOnFloor)
velocity += inputs.Gravity * (float)inputs.Delta * RMovement.GravityModifier;
return velocity;
}
}