diff --git a/Source/MyProject3/SpaceshipPawn.cpp b/Source/MyProject3/SpaceshipPawn.cpp index 630b705..71e0d72 100644 --- a/Source/MyProject3/SpaceshipPawn.cpp +++ b/Source/MyProject3/SpaceshipPawn.cpp @@ -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(Controller)) + { + if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem(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(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(); + + 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(); -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) { diff --git a/Source/MyProject3/SpaceshipPawn.h b/Source/MyProject3/SpaceshipPawn.h index cdc0cba..b056b48 100644 --- a/Source/MyProject3/SpaceshipPawn.h +++ b/Source/MyProject3/SpaceshipPawn.h @@ -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;