ue4-玩家控制器PlayerController、摄像机UCameraComponent

玩家控制器 及 摄像机组件

1、c++中绑定Input事件

自定义控制器AMyPlayerCtrler

1
class AMyPlayerCtrler : public APlayerController

重写几个方法

1
2
3
4
5
// Begin PlayerController interface
virtual void PlayerTick(float DeltaTime) override;
virtual void SetupInputComponent() override; //设置各种键盘、鼠标、触摸等事件的绑定
virtual void ProcessPlayerInput(const float DeltaTime, const bool bGamePaused) override; //带暂停标记的tick,类似游戏暂停呼出菜单时,游戏中不需要接收input
// End PlayerController interface

事件绑定的实现部分

1
2
3
4
5
6
7
8
9
10
11
12
13
void AMyPlayerCtrler::SetupInputComponent()
{
// set up gameplay key bindings
Super::SetupInputComponent();

//对应的MouseClick事件必须在Editor中设置
&AMyPlayerCtrler::OnSetDestinationPressed);
InputComponent->BindAction("MouseClick", IE_Released, this, &AMyPlayerCtrler::OnSetDestinationReleased);

// support touch devices
InputComponent->BindTouch(EInputEvent::IE_Pressed, this, &AMyPlayerCtrler::MoveToTouchLocation);
InputComponent->BindTouch(EInputEvent::IE_Repeat, this, &AMyPlayerCtrler::MoveToTouchLocation);
}

2、Editor中绑定上面几个事件

Edit > Project Settings > Input

img