Basic mouse look implementation (only left/right)
This commit is contained in:
Binary file not shown.
BIN
Content/Input/IA_Look.uasset
LFS
BIN
Content/Input/IA_Look.uasset
LFS
Binary file not shown.
BIN
Content/Input/IA_MouseLook.uasset
LFS
Normal file
BIN
Content/Input/IA_MouseLook.uasset
LFS
Normal file
Binary file not shown.
Binary file not shown.
@@ -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<APlayerController>(Controller))
|
||||
// Store player controller reference
|
||||
PlayerControllerRef = Cast<APlayerController>(Controller);
|
||||
|
||||
if (PlayerControllerRef)
|
||||
{
|
||||
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
|
||||
// Setup input mapping context
|
||||
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(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<FVector2D>();
|
||||
const FVector2D MouseDelta = Value.Get<FVector2D>();
|
||||
|
||||
// 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)
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
Reference in New Issue
Block a user