Begin mouse control implementation

This commit is contained in:
2025-02-16 15:16:22 +05:30
parent 2519db2ced
commit 49895ec6a6
4 changed files with 41 additions and 27 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -24,6 +24,10 @@ ASpaceshipPawn::ASpaceshipPawn()
CameraSpringArm->CameraLagSpeed = 3.0f; CameraSpringArm->CameraLagSpeed = 3.0f;
CameraSpringArm->bEnableCameraRotationLag = true; CameraSpringArm->bEnableCameraRotationLag = true;
CameraSpringArm->CameraRotationLagSpeed = 10.0f; CameraSpringArm->CameraRotationLagSpeed = 10.0f;
CameraSpringArm->bUsePawnControlRotation = false; // Add this line
CameraSpringArm->bInheritPitch = true; // Add this line
CameraSpringArm->bInheritYaw = true; // Add this line
CameraSpringArm->bInheritRoll = false; // Add this line
// Create camera // Create camera
Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera")); Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
@@ -34,8 +38,9 @@ ASpaceshipPawn::ASpaceshipPawn()
TargetThrust = 0.0f; TargetThrust = 0.0f;
bThrottlePressed = false; bThrottlePressed = false;
CurrentVelocity = FVector::ZeroVector; CurrentVelocity = FVector::ZeroVector;
CurrentRotation = GetActorQuat(); CurrentPitch = 0.0f;
TargetRotation = GetActorRotation(); CurrentYaw = 0.0f;
ShipRotation = FRotator::ZeroRotator;
} }
void ASpaceshipPawn::BeginPlay() void ASpaceshipPawn::BeginPlay()
@@ -56,7 +61,7 @@ void ASpaceshipPawn::Tick(float DeltaTime)
{ {
Super::Tick(DeltaTime); Super::Tick(DeltaTime);
// Handle thrust deceleration when not pressing throttle // Handle thrust
if (!bThrottlePressed) if (!bThrottlePressed)
{ {
CurrentThrust = FMath::FInterpTo(CurrentThrust, 0.0f, DeltaTime, ThrustDeceleration); CurrentThrust = FMath::FInterpTo(CurrentThrust, 0.0f, DeltaTime, ThrustDeceleration);
@@ -66,7 +71,17 @@ void ASpaceshipPawn::Tick(float DeltaTime)
CurrentThrust = FMath::FInterpTo(CurrentThrust, TargetThrust, DeltaTime, ThrustAcceleration); CurrentThrust = FMath::FInterpTo(CurrentThrust, TargetThrust, DeltaTime, ThrustAcceleration);
} }
// Calculate movement // Smoothly interpolate to target rotation
FRotator CurrentRotation = GetActorRotation();
FRotator NewRotation = FMath::RInterpTo(
CurrentRotation,
ShipRotation,
DeltaTime,
RotationSpeed
);
SetActorRotation(NewRotation);
// Update movement based on ship's forward vector
FVector ThrustDirection = GetActorForwardVector(); FVector ThrustDirection = GetActorForwardVector();
FVector ThrustForce = ThrustDirection * CurrentThrust; FVector ThrustForce = ThrustDirection * CurrentThrust;
@@ -80,11 +95,6 @@ void ASpaceshipPawn::Tick(float DeltaTime)
FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime); FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime);
SetActorLocation(NewLocation, true); SetActorLocation(NewLocation, true);
// Smooth rotation using quaternion interpolation
FQuat TargetQuat = TargetRotation.Quaternion();
CurrentRotation = FQuat::Slerp(CurrentRotation, TargetQuat, RotationSpeed * DeltaTime);
SetActorRotation(CurrentRotation);
// Debug info // Debug info
if (GEngine) if (GEngine)
{ {
@@ -130,21 +140,24 @@ void ASpaceshipPawn::HandleMouseControl(const FInputActionValue& Value)
{ {
const FVector2D MouseValue = Value.Get<FVector2D>(); const FVector2D MouseValue = Value.Get<FVector2D>();
// Get current rotation // Mouse sensitivity factors
FRotator CurrentRot = GetActorRotation(); const float MouseSensitivityX = 1.0f;
const float MouseSensitivityY = 1.0f;
// Calculate new rotation values // Update current rotation based on mouse input
float NewPitch = CurrentRot.Pitch + (-MouseValue.Y * RotationSpeed * GetWorld()->GetDeltaSeconds()); CurrentYaw += MouseValue.X * MouseSensitivityX;
float NewYaw = CurrentRot.Yaw + (MouseValue.X * RotationSpeed * GetWorld()->GetDeltaSeconds()); CurrentPitch = FMath::ClampAngle(CurrentPitch + (-MouseValue.Y * MouseSensitivityY), -85.0f, 85.0f);
// Clamp pitch to prevent flipping // Create target rotation
NewPitch = FMath::ClampAngle(NewPitch, -85.0f, 85.0f); ShipRotation = FRotator(CurrentPitch, CurrentYaw, 0.0f);
// Set target rotation if (GEngine)
TargetRotation = FRotator(NewPitch, NewYaw, CurrentRot.Roll); {
GEngine->AddOnScreenDebugMessage(-1, 0.0f, FColor::Blue,
// Update camera spring arm rotation to match FString::Printf(TEXT("Mouse Input - X: %.2f, Y: %.2f"), MouseValue.X, MouseValue.Y));
CameraSpringArm->SetRelativeRotation(FRotator(NewPitch * 0.3f, 0.0f, 0.0f)); GEngine->AddOnScreenDebugMessage(-1, 0.0f, FColor::Purple,
FString::Printf(TEXT("Ship Rotation - Pitch: %.2f, Yaw: %.2f"), CurrentPitch, CurrentYaw));
}
} }
void ASpaceshipPawn::HandleFire(const FInputActionValue& Value) void ASpaceshipPawn::HandleFire(const FInputActionValue& Value)

View File

@@ -69,6 +69,7 @@ private:
float TargetThrust; float TargetThrust;
bool bThrottlePressed; bool bThrottlePressed;
FVector CurrentVelocity; FVector CurrentVelocity;
FRotator TargetRotation; FRotator ShipRotation;
FQuat CurrentRotation; float CurrentPitch;
float CurrentYaw;
}; };