Modify to use EnhancedInput
This commit is contained in:
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user