Modify to use EnhancedInput

This commit is contained in:
2025-02-15 21:32:16 +05:30
parent bb4d1bef59
commit 536a938d55
2 changed files with 57 additions and 24 deletions

View File

@@ -2,6 +2,8 @@
#include "GameFramework/SpringArmComponent.h" #include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h" #include "Camera/CameraComponent.h"
#include "Components/StaticMeshComponent.h" #include "Components/StaticMeshComponent.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "SpaceshipProjectile.h" #include "SpaceshipProjectile.h"
ASpaceshipPawn::ASpaceshipPawn() ASpaceshipPawn::ASpaceshipPawn()
@@ -27,6 +29,15 @@ ASpaceshipPawn::ASpaceshipPawn()
void ASpaceshipPawn::BeginPlay() void ASpaceshipPawn::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
// Add Input Mapping Context
if (APlayerController* PlayerController = Cast<APlayerController>(Controller))
{
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
{
Subsystem->AddMappingContext(DefaultMappingContext, 0);
}
}
} }
void ASpaceshipPawn::Tick(float DeltaTime) void ASpaceshipPawn::Tick(float DeltaTime)
@@ -44,37 +55,47 @@ void ASpaceshipPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputCompo
{ {
Super::SetupPlayerInputComponent(PlayerInputComponent); Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis("MoveForward", this, &ASpaceshipPawn::MoveForward); if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent))
PlayerInputComponent->BindAxis("MoveRight", this, &ASpaceshipPawn::MoveRight); {
PlayerInputComponent->BindAxis("Turn", this, &ASpaceshipPawn::Turn); // Binding the Movement action
PlayerInputComponent->BindAxis("LookUp", this, &ASpaceshipPawn::LookUp); EnhancedInputComponent->BindAction(MovementAction, ETriggerEvent::Triggered, this, &ASpaceshipPawn::Move);
PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &ASpaceshipPawn::Fire);
// Binding the Look action
EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &ASpaceshipPawn::Look);
// Binding the Fire action
EnhancedInputComponent->BindAction(FireAction, ETriggerEvent::Triggered, this, &ASpaceshipPawn::Fire);
}
} }
void ASpaceshipPawn::MoveForward(float Value) void ASpaceshipPawn::Move(const FInputActionValue& Value)
{ {
CurrentVelocity.X = FMath::Clamp(Value, -1.0f, 1.0f) * MovementSpeed; const FVector2D MovementVector = Value.Get<FVector2D>();
CurrentVelocity = FVector(
MovementVector.X * MovementSpeed,
MovementVector.Y * MovementSpeed,
0.0f
);
} }
void ASpaceshipPawn::MoveRight(float Value) void ASpaceshipPawn::Look(const FInputActionValue& Value)
{ {
CurrentVelocity.Y = FMath::Clamp(Value, -1.0f, 1.0f) * MovementSpeed; const FVector2D LookAxisVector = Value.Get<FVector2D>();
}
void ASpaceshipPawn::Turn(float Value) // Yaw rotation (left/right)
{ AddActorLocalRotation(FRotator(0.0f, LookAxisVector.X * RotationSpeed * GetWorld()->GetDeltaSeconds(), 0.0f));
AddActorLocalRotation(FRotator(0.0f, Value * RotationSpeed * GetWorld()->GetDeltaSeconds(), 0.0f));
}
void ASpaceshipPawn::LookUp(float Value) // Pitch rotation (up/down) for camera
{ float NewPitch = CameraSpringArm->GetRelativeRotation().Pitch +
float NewPitch = CameraSpringArm->GetRelativeRotation().Pitch + Value * RotationSpeed * GetWorld()->GetDeltaSeconds(); LookAxisVector.Y * RotationSpeed * GetWorld()->GetDeltaSeconds();
NewPitch = FMath::Clamp(NewPitch, -80.0f, 80.0f); NewPitch = FMath::Clamp(NewPitch, -80.0f, 80.0f);
CameraSpringArm->SetRelativeRotation(FRotator(NewPitch, 0.0f, 0.0f)); CameraSpringArm->SetRelativeRotation(FRotator(NewPitch, 0.0f, 0.0f));
} }
void ASpaceshipPawn::Fire() void ASpaceshipPawn::Fire(const FInputActionValue& Value)
{ {
// Implementation remains the same as before
UWorld* World = GetWorld(); UWorld* World = GetWorld();
if (World) if (World)
{ {

View File

@@ -2,6 +2,7 @@
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "GameFramework/Pawn.h" #include "GameFramework/Pawn.h"
#include "InputActionValue.h"
#include "SpaceshipPawn.generated.h" #include "SpaceshipPawn.generated.h"
UCLASS() UCLASS()
@@ -26,18 +27,29 @@ protected:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Components") UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Components")
class UCameraComponent* Camera; class UCameraComponent* Camera;
// Enhanced Input Components
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input")
class UInputMappingContext* DefaultMappingContext;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input")
class UInputAction* MovementAction;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input")
class UInputAction* LookAction;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input")
class UInputAction* FireAction;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement") UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
float MovementSpeed = 1000.0f; float MovementSpeed = 1000.0f;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement") UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
float RotationSpeed = 100.0f; float RotationSpeed = 100.0f;
// Movement functions // Input functions
void MoveForward(float Value); void Move(const FInputActionValue& Value);
void MoveRight(float Value); void Look(const FInputActionValue& Value);
void Turn(float Value); void Fire(const FInputActionValue& Value);
void LookUp(float Value);
void Fire();
private: private:
FVector CurrentVelocity; FVector CurrentVelocity;