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

View File

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