broken sloped slide
All checks were successful
Create tag and build when new code gets to main / BumpTag (push) Successful in 20s
Create tag and build when new code gets to main / Export (push) Successful in 9m32s

This commit is contained in:
2026-01-13 23:52:44 +01:00
parent 30b4d1a2eb
commit e32dac9e6e
4 changed files with 136 additions and 10 deletions

View File

@@ -146,6 +146,13 @@ public partial class PlayerController : CharacterBody3D
public float GroundSlideJumpMultiplier = 1.0f;
[Export(PropertyHint.Range, "0,1,0.01,or_greater")]
public float GroundSlideJumpSpeedFactor;
[Export(PropertyHint.Range, "0,1,0.01,or_greater")]
public float GroundSlideDownSlopeAcceleration = 0.1f;
[Export(PropertyHint.Range, "0,100,0.1,or_greater")]
public float GroundSlideDownSlopeMaxSpeed = 50f;
[Export(PropertyHint.Range, "1,10,0.1,or_greater")]
public float GroundSlideSlopeMagnetism = 2f;
[ExportSubgroup("Air glide")]
[Export]
@@ -265,6 +272,7 @@ public partial class PlayerController : CharacterBody3D
private StateChartState _grounded;
private StateChartState _airborne;
private StateChartState _coyoteEnabled;
private StateChartState _jumping;
private StateChartState _simpleJump;
private StateChartState _doubleJump;
private StateChartState _mantling;
@@ -361,6 +369,7 @@ public partial class PlayerController : CharacterBody3D
_airborne = StateChartState.Of(GetNode("StateChart/Root/Movement/Airborne"));
_coyoteEnabled = StateChartState.Of(GetNode("StateChart/Root/Movement/Airborne/CoyoteEnabled"));
// _doubleJumpEnabled = StateChartState.Of(GetNode("StateChart/Root/Movement/Airborne/DoubleJumpEnabled"));
_jumping = StateChartState.Of(GetNode("StateChart/Root/Movement/Jump"));
_simpleJump = StateChartState.Of(GetNode("StateChart/Root/Movement/Jump/SimpleJump"));
_doubleJump = StateChartState.Of(GetNode("StateChart/Root/Movement/Jump/DoubleJump"));
_mantling = StateChartState.Of(GetNode("StateChart/Root/Movement/Mantling"));
@@ -593,7 +602,7 @@ public partial class PlayerController : CharacterBody3D
};
StairsSystem.UpStairsCheckResult upStairsCheckResult = StairsSystem.SnapUpStairsCheck(upStairsCheckParams);
if (upStairsCheckResult.UpdateRequired)
if (upStairsCheckResult.UpdateRequired && !_jumping.Active)
{
upStairsCheckResult.Update(this);
}
@@ -1092,7 +1101,9 @@ public partial class PlayerController : CharacterBody3D
public void OnJumpStarted(float verticalVelocity)
{
_framesSinceJumpAtApex = 0;
SetVerticalVelocity(verticalVelocity*_jumpStrengthMultiplier);
var angle = GetFloorAngle();
var floorAngleFactor = angle > 1 ? 1 : 1 + angle;
SetVerticalVelocity(verticalVelocity*_jumpStrengthMultiplier*floorAngleFactor);
_jumpStrengthMultiplier = 1.0f;
}
public void OnSimpleJumpStarted()
@@ -1246,24 +1257,70 @@ public partial class PlayerController : CharacterBody3D
_playerState.SendEvent("slide_released");
}
public record SlopeRecord(
Vector3 Position,
Vector3 Normal,
Vector3 Direction,
float AngleRadians
);
public Vector3 GetGroundPosition()
{
return GroundDetector.GetCollisionPoint(0);
}
public Vector3 GetGroundNormal()
{
return GroundDetector.GetCollisionNormal(0);
}
public SlopeRecord GetSlope()
{
var position = GetGroundPosition();
var normal = GetGroundNormal();
var angle = normal.AngleTo(Vector3.Up);
var vectorInPlane = normal.Cross(Vector3.Up).Normalized();
var direction = normal.Cross(vectorInPlane).Normalized();
return new SlopeRecord(position, normal, direction, angle);
}
public void SlideStarted()
{
_targetSpeed = Velocity.Length();
}
public void SlideOnGround(float delta)
{
// Store current velocity
var currentVelocity = Velocity.Length();
// Going down slope?
var (position, normal, slopeDirection, slopeAngleRadians) = GetSlope();
// Change velocity based on Input
var horizontalVelocity = ComputeHVelocity(delta, AccelerationGroundSlide, DecelerationGroundSlide);
var newVelocity = new Vector3(horizontalVelocity.X, Velocity.Y, horizontalVelocity.Z);
Velocity = newVelocity.Normalized() * currentVelocity; // prevent from losing momentum
var newVelocityDirection = new Vector3(horizontalVelocity.X, 0, horizontalVelocity.Z).Normalized();
// var redirectedVelocity = newVelocityDirection.Slide(normal);
var redirectedVelocity = newVelocityDirection;
var speedFactorFromDownSlope = 1f;
if (slopeAngleRadians > Mathf.Epsilon)
{
var slopeHDirection = new Vector3(slopeDirection.X, 0, slopeDirection.Z);
redirectedVelocity = newVelocityDirection.Lerp(slopeHDirection, delta * GroundSlideSlopeMagnetism);
var angleBetweenVelocityAndSlope = redirectedVelocity.AngleTo(slopeHDirection);
var cosAngle = Mathf.Cos(angleBetweenVelocityAndSlope);
var redirectedVVelocity = slopeDirection.Y * cosAngle;
redirectedVelocity = new Vector3(redirectedVelocity.X, redirectedVVelocity, redirectedVelocity.Z);
speedFactorFromDownSlope = Velocity.Length() > GroundSlideDownSlopeMaxSpeed ? 1f : 1f + GroundSlideDownSlopeAcceleration * GetFloorAngle() * cosAngle;
}
// Preserve velocity when changing direction
var finalVelocity = redirectedVelocity.Normalized() * currentVelocity * speedFactorFromDownSlope;
Velocity = finalVelocity;
}
public void HandleGroundSlide(float delta)
{
// GD.Print(GetFloorAngle());
SlideOnGround(delta);
if (MantleSystem.IsMantlePossible && IsPlayerInputtingForward()) _playerState.SendEvent("mantle");
if (!isOnFloorCustom())
_playerState.SendEvent("start_falling");
}
@@ -1593,6 +1650,4 @@ public partial class PlayerController : CharacterBody3D
DashIndicatorNode.LookAt(WeaponSystem.GlobalPosition);
}
}
}