Modify movement to feel more intuitive (align with direction faster)
This commit is contained in:
@@ -82,30 +82,6 @@ void ASpaceshipPawn::Tick(float DeltaTime)
|
||||
{
|
||||
Super::Tick(DeltaTime);
|
||||
|
||||
// Handle thrust
|
||||
if (!bThrottlePressed)
|
||||
{
|
||||
CurrentThrust = FMath::FInterpTo(CurrentThrust, 0.0f, DeltaTime, ThrustDeceleration);
|
||||
}
|
||||
else
|
||||
{
|
||||
CurrentThrust = FMath::FInterpTo(CurrentThrust, TargetThrust, DeltaTime, ThrustAcceleration);
|
||||
}
|
||||
|
||||
// Update movement based on ship's forward vector
|
||||
FVector ThrustDirection = GetActorForwardVector();
|
||||
FVector ThrustForce = ThrustDirection * CurrentThrust;
|
||||
|
||||
// Apply drag
|
||||
FVector DragForce = -CurrentVelocity * DragCoefficient;
|
||||
|
||||
// Update velocity
|
||||
CurrentVelocity += (ThrustForce + DragForce) * DeltaTime;
|
||||
|
||||
// Update position
|
||||
FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime);
|
||||
SetActorLocation(NewLocation, true);
|
||||
|
||||
// Debug info
|
||||
if (GEngine)
|
||||
{
|
||||
@@ -140,6 +116,67 @@ void ASpaceshipPawn::Tick(float DeltaTime)
|
||||
FQuat NewQuat = FQuat::Slerp(CurrentQuat, TargetQuat, RotationSpeed * DeltaTime);
|
||||
SetActorRotation(NewQuat);
|
||||
|
||||
// Get the current forward vector
|
||||
FVector CurrentDirection = GetActorForwardVector();
|
||||
float CurrentSpeed = CurrentVelocity.Size();
|
||||
|
||||
// Handle thrust and velocity direction
|
||||
if (!bThrottlePressed)
|
||||
{
|
||||
// Decelerate when not thrusting
|
||||
CurrentThrust = FMath::FInterpTo(CurrentThrust, 0.0f, DeltaTime, ThrustDeceleration);
|
||||
bWasThrottlePressed = false;
|
||||
|
||||
// Maintain current velocity direction when not thrusting
|
||||
DesiredVelocityDirection = CurrentVelocity.GetSafeNormal();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Accelerate when thrusting
|
||||
CurrentThrust = FMath::FInterpConstantTo(CurrentThrust, TargetThrust, DeltaTime, ThrustAcceleration);
|
||||
|
||||
if (!bWasThrottlePressed)
|
||||
{
|
||||
// Just started thrusting - blend between current and new direction
|
||||
if (!CurrentVelocity.IsNearlyZero())
|
||||
{
|
||||
// Blend between current velocity direction and new direction
|
||||
FVector CurrentDir = CurrentVelocity.GetSafeNormal();
|
||||
DesiredVelocityDirection = FMath::Lerp(CurrentDir, CurrentDirection, 1.0f - DirectionalInertia);
|
||||
DesiredVelocityDirection.Normalize();
|
||||
}
|
||||
else
|
||||
{
|
||||
// If nearly stationary, use new direction
|
||||
DesiredVelocityDirection = CurrentDirection;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Continuously blend towards current facing direction while thrusting
|
||||
DesiredVelocityDirection = FMath::VInterpNormalRotationTo(
|
||||
DesiredVelocityDirection,
|
||||
CurrentDirection,
|
||||
DeltaTime,
|
||||
VelocityAlignmentSpeed
|
||||
);
|
||||
}
|
||||
bWasThrottlePressed = true;
|
||||
}
|
||||
|
||||
// Calculate thrust force
|
||||
FVector ThrustForce = CurrentDirection * CurrentThrust;
|
||||
|
||||
// Apply drag
|
||||
FVector DragForce = -CurrentVelocity * DragCoefficient;
|
||||
|
||||
// Update velocity with forces
|
||||
CurrentVelocity += (ThrustForce + DragForce) * DeltaTime;
|
||||
|
||||
// Update position
|
||||
FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime);
|
||||
SetActorLocation(NewLocation, true);
|
||||
|
||||
// Update spring arm rotation to match ship with smooth interpolation
|
||||
if (CameraSpringArm)
|
||||
{
|
||||
|
||||
@@ -60,6 +60,13 @@ protected:
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
|
||||
float DragCoefficient = 0.05f;
|
||||
|
||||
// How quickly velocity aligns with new direction
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement", meta = (ClampMin = "0.0", ClampMax = "10.0"))
|
||||
float VelocityAlignmentSpeed = 4.0f;
|
||||
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement", meta = (ClampMin = "0.0", ClampMax = "1.0", ToolTip = "How much of the current velocity is maintained when changing direction (0 = none, 1 = full)"))
|
||||
float DirectionalInertia = 0.3f;
|
||||
|
||||
// Input functions
|
||||
void HandleThrottleStarted(const FInputActionValue& Value);
|
||||
void HandleThrottleReleased(const FInputActionValue& Value);
|
||||
@@ -81,4 +88,7 @@ private:
|
||||
FVector2D MouseDeltaSmoothed;
|
||||
FVector2D LastMouseDelta;
|
||||
float MouseSmoothingSpeed = 10.0f;
|
||||
|
||||
bool bWasThrottlePressed;
|
||||
FVector DesiredVelocityDirection;
|
||||
};
|
||||
Reference in New Issue
Block a user