feat: 增加输入系统

This commit is contained in:
meishibiezb
2026-03-14 23:59:25 +08:00
parent af559cb60b
commit b2cb9a4d0c
6 changed files with 133 additions and 31 deletions

View File

@@ -0,0 +1,61 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "EnhancedInputSubsystems.h"
#include "EnhancedInputComponent.h"
#include "MyCharacter.h"
void AMyCharacter::Move(const FInputActionValue& Value)
{
auto f2d = Value.Get<FVector2D>();
AddMovementInput(GetActorRotation().Vector());
}
// Sets default values
AMyCharacter::AMyCharacter()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void AMyCharacter::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
auto PlayerController = Cast<APlayerController>(GetController());
UEnhancedInputLocalPlayerSubsystem* Subsystem;
if (PlayerController)
{
Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer());
}
if (Subsystem && DefaultMapping)
{
Subsystem->AddMappingContext(DefaultMapping, 0);
}
auto EnhancedInputComponent = Cast<UEnhancedInputComponent>(PlayerInputComponent);
if (EnhancedInputComponent)
{
if (MoveAction) {
EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AMyCharacter::Move);
}
}
}

View File

@@ -0,0 +1,37 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "MyCharacter.generated.h"
UCLASS()
class LONESE_API AMyCharacter : public ACharacter
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input")
class UInputMappingContext* DefaultMapping;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input")
class UInputAction* MoveAction;
UFUNCTION(BlueprintCallable, Category = "Move")
void Move(const FInputActionValue& Value);
// Sets default values for this character's properties
AMyCharacter();
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
private:
};

View File

@@ -10,5 +10,5 @@ APawn* AMyPlayerController::GetOwnedPawn()
void AMyPlayerController::BeginPlay()
{
//GetPawn()->SetActorLocation(FVector(0.f, 0.f, 300.f));
}
Super::BeginPlay();
}

View File

@@ -22,5 +22,4 @@ protected:
virtual void BeginPlay() override;
private:
};

View File

@@ -8,7 +8,7 @@ public class lonese : ModuleRules
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "EnhancedInput" });
PrivateDependencyModuleNames.AddRange(new string[] { });