在UE中,Pawn是一种可以被控制的Actor,我们在创建C++类时,选择Pawn作为父类,可以看到说明,Pawn可以接受来自Controller的输入。
![](https://img-blog.csdnimg.cn/img_convert/189e07d1f3bc69f2a682d999e31f94a0.png)
新建完类之后,可以看到新建的类是继承APawn的,进入到APawn里面,就能看到Pawn也是继承Actor的,所以说Pawn是一种可被控制的Actor
![](https://img-blog.csdnimg.cn/img_convert/5f7223d07b3ae532e21573d7c5a6f44f.png)
在我们实现输入控制之前,需要先在编辑器的项目设置中,找到Input一栏,然后有Axis Mappings和Action Mappings
![](https://img-blog.csdnimg.cn/img_convert/ca7de08a543369bbbab15e8109b1abf6.png)
分别是轴映射和操作映射,轴映射是用来控制Pawn前进后退转向的,操作一般是用来控制跳跃、蹲伏等
先点+号进行添加,如果是用的初学者内容包的模板,FPS模板TPS模板的话,这里都会有添加好的配置。
比如MoveForward向前移动,配置键盘W,按下往前,S往后,右边的scale则往前为1.0,向后为-1.0
配置好之后回到vs中,开始进行绑定
先在.h文件中声明三个方法
private:
void MoveForward(float Value);
void MoveRight(float Value);
void StartJump();
void OverJump();
再到cpp中进行实现
void ACritter::MoveForward(float Value)
{
CurrentVelocity.X = FMath::Clamp(Value, -1.f, 1.f) * MaxSpeed;
}
void ACritter::MoveRight(float Value)
{
CurrentVelocity.Y = FMath::Clamp(Value, -1.f, 1.f) * MaxSpeed;
}
void ACritter::StartJump()
{
}
void ACritter::OverJump()
{
}
接下来就是对映射进行绑定,在自动生成的方法里面有一个SetupPlayerInputComponent方法,通过他来进行绑定
// Called to bind functionality to input
void ACritter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis("MoveForward", this, &ACritter::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &ACritter::MoveRight);
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACritter::StartJump);
PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACritter::OverJump);
}
可以看到轴映射是各绑定一个,这是因为轴映射绑定之后,每帧会调用绑定的函数,在没有按键的时候,也会一直调用,只不过传的值为0.0f;而操作映射则不一样,当按下按键触发一次,抬起触发一次,所以按下和抬起都需要进行绑定。