diff --git a/Content/Blueprints/BP_SpaceshipPawn.uasset b/Content/Blueprints/BP_SpaceshipPawn.uasset index 24f4f92..3bbcb3b 100644 --- a/Content/Blueprints/BP_SpaceshipPawn.uasset +++ b/Content/Blueprints/BP_SpaceshipPawn.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:eee80d735172d622902083842d0708c03911b8deddc22831cf9a72aefb0d18a3 -size 33172 +oid sha256:354c71aaba719e174ba9461bba1c2198e54cf76cb44d855b38236971de12fe45 +size 33170 diff --git a/Content/Input/IA_Look.uasset b/Content/Input/IA_Look.uasset deleted file mode 100644 index dab6a58..0000000 --- a/Content/Input/IA_Look.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:63af299b2e9230387fcca7bb37fb475e0dc78539db2942b220104bc3b5fbc359 -size 2032 diff --git a/Content/Input/IA_MouseLook.uasset b/Content/Input/IA_MouseLook.uasset new file mode 100644 index 0000000..e355ac0 --- /dev/null +++ b/Content/Input/IA_MouseLook.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bfc00aea89da848e56155754194a2bc52f0bff9825666aeb8b1dd1ccec6ec348 +size 2075 diff --git a/Content/Input/IMC_Spaceship.uasset b/Content/Input/IMC_Spaceship.uasset index ad723fc..9dfdaf5 100644 --- a/Content/Input/IMC_Spaceship.uasset +++ b/Content/Input/IMC_Spaceship.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:aaae1086867188388e75ae7dc59d97ac8738922e3de37fa72102cc35683ca122 -size 5640 +oid sha256:eb9fb49c50ae73afd719ed9f795e34340fa89d6671e2963d0a25b93f31589461 +size 6259 diff --git a/Source/MyProject3/SpaceshipPawn.cpp b/Source/MyProject3/SpaceshipPawn.cpp index c8d7f78..ac983ba 100644 --- a/Source/MyProject3/SpaceshipPawn.cpp +++ b/Source/MyProject3/SpaceshipPawn.cpp @@ -5,6 +5,9 @@ #include "EnhancedInputComponent.h" #include "EnhancedInputSubsystems.h" #include "SpaceshipProjectile.h" +#include "Kismet/GameplayStatics.h" +#include "GameFramework/GameUserSettings.h" + ASpaceshipPawn::ASpaceshipPawn() { @@ -40,20 +43,36 @@ ASpaceshipPawn::ASpaceshipPawn() CurrentVelocity = FVector::ZeroVector; CurrentPitch = 0.0f; CurrentYaw = 0.0f; - ShipRotation = FRotator::ZeroRotator; + TargetRotation = FRotator::ZeroRotator; } void ASpaceshipPawn::BeginPlay() { Super::BeginPlay(); - // Add Input Mapping Context - if (APlayerController* PlayerController = Cast(Controller)) + // Store player controller reference + PlayerControllerRef = Cast(Controller); + + if (PlayerControllerRef) { - if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem(PlayerController->GetLocalPlayer())) + // Setup input mapping context + if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem(PlayerControllerRef->GetLocalPlayer())) { Subsystem->AddMappingContext(DefaultMappingContext, 0); } + + // Setup mouse capture and fullscreen + PlayerControllerRef->SetShowMouseCursor(false); + PlayerControllerRef->SetInputMode(FInputModeGameOnly()); + + // Request fullscreen mode using GameUserSettings + UGameUserSettings* GameUserSettings = UGameUserSettings::GetGameUserSettings(); + if (GameUserSettings) + { + GameUserSettings->SetFullscreenMode(EWindowMode::Fullscreen); + GameUserSettings->SetScreenResolution(FIntPoint(1920, 1080)); // Adjust the resolution if needed + GameUserSettings->ApplySettings(false); + } } } @@ -71,16 +90,6 @@ void ASpaceshipPawn::Tick(float DeltaTime) CurrentThrust = FMath::FInterpTo(CurrentThrust, TargetThrust, DeltaTime, ThrustAcceleration); } - // 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; @@ -103,6 +112,57 @@ void ASpaceshipPawn::Tick(float DeltaTime) GEngine->AddOnScreenDebugMessage(-1, 0.0f, FColor::Green, FString::Printf(TEXT("Velocity: %.2f"), CurrentVelocity.Size())); } + + // Smooth mouse movement + MouseDeltaSmoothed = FMath::Vector2DInterpTo( + MouseDeltaSmoothed, + LastMouseDelta, + DeltaTime, + MouseSmoothingSpeed + ); + + // Update rotation based on smoothed mouse movement + CurrentYaw += MouseDeltaSmoothed.X * MouseSensitivity * DeltaTime * 60.0f; // Multiply by 60 to normalize for frame rate + CurrentPitch = FMath::ClampAngle( + CurrentPitch + (-MouseDeltaSmoothed.Y * MouseSensitivity * DeltaTime * 60.0f), + -85.0f, + 85.0f + ); + + // Set target rotation + TargetRotation = FRotator(CurrentPitch, CurrentYaw, 0.0f); + + // Smoothly interpolate to target rotation using quaternions for better interpolation + FQuat CurrentQuat = GetActorQuat(); + FQuat TargetQuat = TargetRotation.Quaternion(); + FQuat NewQuat = FQuat::Slerp(CurrentQuat, TargetQuat, RotationSpeed * DeltaTime); + SetActorRotation(NewQuat); + + // Update spring arm rotation to match ship with smooth interpolation + if (CameraSpringArm) + { + FRotator SpringArmRotation = CameraSpringArm->GetComponentRotation(); + FRotator TargetSpringArmRotation = NewQuat.Rotator(); + FRotator NewSpringArmRotation = FMath::RInterpTo( + SpringArmRotation, + TargetSpringArmRotation, + DeltaTime, + RotationSpeed + ); + CameraSpringArm->SetWorldRotation(NewSpringArmRotation); + } + + // Reset mouse delta for next frame + LastMouseDelta = FVector2D::ZeroVector; + + // Debug info + if (GEngine) + { + GEngine->AddOnScreenDebugMessage(-1, 0.0f, FColor::Yellow, + FString::Printf(TEXT("Smoothed Mouse Delta: X=%.2f Y=%.2f"), MouseDeltaSmoothed.X, MouseDeltaSmoothed.Y)); + GEngine->AddOnScreenDebugMessage(-1, 0.0f, FColor::Green, + FString::Printf(TEXT("Current Rotation: P=%.2f Y=%.2f"), CurrentPitch, CurrentYaw)); + } } void ASpaceshipPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) @@ -116,7 +176,7 @@ void ASpaceshipPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputCompo EnhancedInputComponent->BindAction(ThrottleAction, ETriggerEvent::Completed, this, &ASpaceshipPawn::HandleThrottleReleased); // Bind mouse control - EnhancedInputComponent->BindAction(MouseControlAction, ETriggerEvent::Started, this, &ASpaceshipPawn::HandleMouseControl); + EnhancedInputComponent->BindAction(MouseControlAction, ETriggerEvent::Triggered, this, &ASpaceshipPawn::HandleMouseLook); // Bind fire action EnhancedInputComponent->BindAction(FireAction, ETriggerEvent::Triggered, this, &ASpaceshipPawn::HandleFire); @@ -136,28 +196,12 @@ void ASpaceshipPawn::HandleThrottleReleased(const FInputActionValue& Value) bThrottlePressed = false; } -void ASpaceshipPawn::HandleMouseControl(const FInputActionValue& Value) +void ASpaceshipPawn::HandleMouseLook(const FInputActionValue& Value) { - const FVector2D MouseValue = Value.Get(); + const FVector2D MouseDelta = Value.Get(); - // Mouse sensitivity factors - const float MouseSensitivityX = 1.0f; - const float MouseSensitivityY = 1.0f; - - // Update current rotation based on mouse input - CurrentYaw += MouseValue.X * MouseSensitivityX; - CurrentPitch = FMath::ClampAngle(CurrentPitch + (-MouseValue.Y * MouseSensitivityY), -85.0f, 85.0f); - - // Create target rotation - ShipRotation = FRotator(CurrentPitch, CurrentYaw, 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)); - } + // Smoothly interpolate mouse delta + LastMouseDelta = MouseDelta; } void ASpaceshipPawn::HandleFire(const FInputActionValue& Value) diff --git a/Source/MyProject3/SpaceshipPawn.h b/Source/MyProject3/SpaceshipPawn.h index fe59130..1fefc60 100644 --- a/Source/MyProject3/SpaceshipPawn.h +++ b/Source/MyProject3/SpaceshipPawn.h @@ -54,13 +54,16 @@ protected: UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement") float RotationSpeed = 100.0f; + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement") + float MouseSensitivity = 2.0f; + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement") float DragCoefficient = 0.1f; // Input functions void HandleThrottleStarted(const FInputActionValue& Value); void HandleThrottleReleased(const FInputActionValue& Value); - void HandleMouseControl(const FInputActionValue& Value); + void HandleMouseLook(const FInputActionValue& Value); void HandleFire(const FInputActionValue& Value); private: @@ -69,7 +72,13 @@ private: float TargetThrust; bool bThrottlePressed; FVector CurrentVelocity; - FRotator ShipRotation; + FRotator TargetRotation; + float CurrentPitch; float CurrentYaw; + APlayerController* PlayerControllerRef; + + FVector2D MouseDeltaSmoothed; + FVector2D LastMouseDelta; + float MouseSmoothingSpeed = 10.0f; }; \ No newline at end of file