您的位置:首页 > 编程语言 > C语言/C++

【UE4/C++】绑定输入响应

2015-10-19 22:13 393 查看
.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')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UE4