【UE4/C++】綁定輸入響應


.h
#include "GameFramework/Pawn.h"
#include "MyPawn.generated.h"

UCLASS()
class PROJ3_API AMyPawn : public APawn
{
GENERATED_BODY()

public:
// Sets default values for this pawn's properties
AMyPawn();

// Called when the game starts or when spawned
virtual void BeginPlay() override;

// Called every frame
virtual void Tick( float DeltaSeconds ) override;

// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;

void Move_XAxis(float AxisValue);
void Move_YAxis(float AxisValue);
void StartGrowing();
void StopGrowing();
FVector CurrentVelocity;
bool bGrowing;
};


.cpp
#include "Proj3.h"
#include "MyPawn.h"


// Sets default values
AMyPawn::AMyPawn()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
AutoPossessPlayer = EAutoReceiveInput::Player0;
}

// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
Super::BeginPlay();

}

// Called every frame
void AMyPawn::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}

// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
Super::SetupPlayerInputComponent(InputComponent);
InputComponent->BindAction("Fire", IE_Pressed, this, &AMyPawn::StartGrowing);
InputComponent->BindAction("Fire", IE_Released, this, &AMyPawn::StopGrowing);

InputComponent->BindAxis("MoveX", this, &AMyPawn::Move_XAxis);
InputComponent->BindAxis("MoveY", this, &AMyPawn::Move_YAxis);

}
void AMyPawn::Move_XAxis(float AxisValue)
{
CurrentVelocity.X = FMath::Clamp(AxisValue, -1.0f, 1.0f)*100.0f;
}
void AMyPawn::Move_YAxis(float AxisValue)
{
CurrentVelocity.Y = FMath::Clamp(AxisValue, -1.0f, 1.0f)*100.0f;
}
void AMyPawn::StartGrowing()
{
bGrowing = true;
}
void AMyPawn::StopGrowing()
{
bGrowing = false;
}
關鍵點: 1.AutoPossessPlayer = EAutoReceiveInput::Player0; 2.SetupPlayerInputComponent. 對於BindAction和BindAxis的第一個參數,指的是在項目中設置的鍵盤映射(例子中的項目設定了一個鍵'Fire')

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com