在线编译器 C

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "EnhancedInput" }); #pragma once #include "CoreMinimal.h" #include "GameFramework/Character.h" #include "InputActionValue.h" #include "AMyPlayer.generated.h" UCLASS() class MYPROJECT_API AMyPlayer : public ACharacter { GENERATED_BODY() public: AMyPlayer(); /** Input Actions */ UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input") class UInputMappingContext* DefaultMappingContext; UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input") class UInputAction* MoveAction; UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input") class UInputAction* LookAction; protected: virtual void BeginPlay() override; virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; /** Input Handlers */ void Move(const FInputActionValue& Value); void Look(const FInputActionValue& Value); }; #include "AMyPlayer.h" #include "EnhancedInputComponent.h" #include "EnhancedInputSubsystems.h" AMyPlayer::AMyPlayer() { PrimaryActorTick.bCanEverTick = false; } void AMyPlayer::BeginPlay() { Super::BeginPlay(); // Add Input Mapping Context if (APlayerController* PC = Cast<APlayerController>(GetController())) { if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PC->GetLocalPlayer())) { Subsystem->AddMappingContext(DefaultMappingContext, 0); } } } void AMyPlayer::Move(const FInputActionValue& Value) { FVector2D MovementVector = Value.Get<FVector2D>(); if (Controller != nullptr) { // Move based on where the player is looking const FRotator Rotation = Controller->GetControlRotation(); const FRotator YawRotation(0, Rotation.Yaw, 0); const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X); const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y); AddMovementInput(ForwardDirection, MovementVector.Y); AddMovementInput(RightDirection, MovementVector.X); } } void AMyPlayer::Look(const FInputActionValue& Value) { FVector2D LookAxisVector = Value.Get<FVector2D>(); if (Controller != nullptr) { AddControllerYawInput(LookAxisVector.X); AddControllerPitchInput(LookAxisVector.Y); } } void AMyPlayer::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) { if (UEnhancedInputComponent* EIC = Cast<UEnhancedInputComponent>(PlayerInputComponent)) { EIC->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AMyPlayer::Move); EIC->BindAction(LookAction, ETriggerEvent::Triggered, this, &AMyPlayer::Look); } }
This code defines a custom Unreal Engine character class AMyPlayer that uses the Enhanced Input system. It sets up an input mapping context in BeginPlay, binds Move and Look actions in SetupPlayerInputComponent, and implements movement relative to the controller's yaw and camera look input.

- The first line is a Build.cs snippet pasted into the header; it does not belong in a .h file and will cause a compile error. Check where your file boundaries are.
- In BeginPlay, the mapping context is added only if GetController() already returns a valid controller. At BeginPlay the controller may not be possessed yet, so consider whether this timing is reliable for your setup.