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 "Camera/CameraComponent.h"
#include "Components/StaticMeshComponent.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "SpaceshipProjectile.h"
ASpaceshipPawn::ASpaceshipPawn()
@@ -27,6 +29,15 @@ ASpaceshipPawn::ASpaceshipPawn()
void ASpaceshipPawn::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)
@@ -44,37 +55,47 @@ void ASpaceshipPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputCompo
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis("MoveForward", this, &ASpaceshipPawn::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &ASpaceshipPawn::MoveRight);
PlayerInputComponent->BindAxis("Turn", this, &ASpaceshipPawn::Turn);
PlayerInputComponent->BindAxis("LookUp", this, &ASpaceshipPawn::LookUp);
PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &ASpaceshipPawn::Fire);
if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent))
{
// Binding the Movement action
EnhancedInputComponent->BindAction(MovementAction, ETriggerEvent::Triggered, this, &ASpaceshipPawn::Move);
// 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)
{
AddActorLocalRotation(FRotator(0.0f, Value * RotationSpeed * GetWorld()->GetDeltaSeconds(), 0.0f));
}
// Yaw rotation (left/right)
AddActorLocalRotation(FRotator(0.0f, LookAxisVector.X * RotationSpeed * GetWorld()->GetDeltaSeconds(), 0.0f));
void ASpaceshipPawn::LookUp(float Value)
{
float NewPitch = CameraSpringArm->GetRelativeRotation().Pitch + Value * RotationSpeed * GetWorld()->GetDeltaSeconds();
// Pitch rotation (up/down) for camera
float NewPitch = CameraSpringArm->GetRelativeRotation().Pitch +
LookAxisVector.Y * RotationSpeed * GetWorld()->GetDeltaSeconds();
NewPitch = FMath::Clamp(NewPitch, -80.0f, 80.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();
if (World)
{

View File

@@ -2,6 +2,7 @@
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "InputActionValue.h"
#include "SpaceshipPawn.generated.h"
UCLASS()
@@ -26,18 +27,29 @@ protected:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Components")
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")
float MovementSpeed = 1000.0f;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement")
float RotationSpeed = 100.0f;
// Movement functions
void MoveForward(float Value);
void MoveRight(float Value);
void Turn(float Value);
void LookUp(float Value);
void Fire();
// Input functions
void Move(const FInputActionValue& Value);
void Look(const FInputActionValue& Value);
void Fire(const FInputActionValue& Value);
private:
FVector CurrentVelocity;