玩家控制器PlayerController、摄像机UCameraComponent

来源:互联网 发布:同步网络平台 编辑:程序博客网 时间:2024/06/05 08:49

1、c++中绑定Input事件

自定义控制器AMyPlayerCtrler

class AMyPlayerCtrler : public APlayerController

重写几个方法

    // 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

事件绑定的实现部分

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

Alt text

%0A%23%20%u73A9%u5BB6%u63A7%u5236%u5668PlayerController%u3001%u6444%u50CF%u673AUCameraComponent%0A@%28UnrealEngine4%29%5Bue4%2C%20%u63A7%u5236%u5668%2C%20%u6444%u50CF%u673A%5D%0A%0A%5Btoc%5D%0A%0A%23%23%201%u3001c++%u4E2D%u7ED1%u5B9AInput%u4E8B%u4EF6%0A%u81EA%u5B9A%u4E49%u63A7%u5236%u5668AMyPlayerCtrler%0A%60%60%60cpp%0Aclass%20AMyPlayerCtrler%20%3A%20public%20APlayerController%0A%60%60%60%0A%u91CD%u5199%u51E0%u4E2A%u65B9%u6CD5%0A%60%60%60cpp%0A%20%20%20%20//%20Begin%20PlayerController%20interface%0A%20%20%20%20virtual%20void%20PlayerTick%28float%20DeltaTime%29%20override%3B%0A%20%20%20%20virtual%20void%20SetupInputComponent%28%29%20override%3B%20%20//%u8BBE%u7F6E%u5404%u79CD%u952E%u76D8%u3001%u9F20%u6807%u3001%u89E6%u6478%u7B49%u4E8B%u4EF6%u7684%u7ED1%u5B9A%0A%20%20%20%20virtual%20void%20ProcessPlayerInput%28const%20float%20DeltaTime%2C%20const%20bool%20bGamePaused%29%20override%3B%20//%u5E26%u6682%u505C%u6807%u8BB0%u7684tick%uFF0C%u7C7B%u4F3C%u6E38%u620F%u6682%u505C%u547C%u51FA%u83DC%u5355%u65F6%uFF0C%u6E38%u620F%u4E2D%u4E0D%u9700%u8981%u63A5%u6536input%0A%20%20%20%20//%20End%20PlayerController%20interface%0A%60%60%60%0A%u4E8B%u4EF6%u7ED1%u5B9A%u7684%u5B9E%u73B0%u90E8%u5206%0A%60%60%60cpp%0Avoid%20AMyPlayerCtrler%3A%3ASetupInputComponent%28%29%0A%7B%0A%09//%20set%20up%20gameplay%20key%20bindings%0A%09Super%3A%3ASetupInputComponent%28%29%3B%0A%09%0A%09//%u5BF9%u5E94%u7684MouseClick%u4E8B%u4EF6%u5FC5%u987B%u5728Editor%u4E2D%u8BBE%u7F6E%0A%09%26AMyPlayerCtrler%3A%3AOnSetDestinationPressed%29%3B%0A%09InputComponent-%3EBindAction%28%22MouseClick%22%2C%20IE_Released%2C%20this%2C%20%26AMyPlayerCtrler%3A%3AOnSetDestinationReleased%29%3B%0A%0A%09//%20support%20touch%20devices%20%0A%09InputComponent-%3EBindTouch%28EInputEvent%3A%3AIE_Pressed%2C%20this%2C%20%26AMyPlayerCtrler%3A%3AMoveToTouchLocation%29%3B%0A%09InputComponent-%3EBindTouch%28EInputEvent%3A%3AIE_Repeat%2C%20this%2C%20%26AMyPlayerCtrler%3A%3AMoveToTouchLocation%29%3B%0A%7D%0A%60%60%60%0A%0A%23%23%202%u3001Editor%u4E2D%u7ED1%u5B9A%u4E0A%u9762%u51E0%u4E2A%u4E8B%u4EF6%0A%3EEdit%20%3E%20Project%20Settings%20%3E%20Input%0A%21%5BAlt%20text%5D%28./aa.png%29%0A%0A
0 0
原创粉丝点击