keywords: [UE4]Controller and Input Related

How to get Touch Position on screen
  1. New C++ class inherit from GameViewportClient

  2. Project Settings -> Engine -> General Settings -> Default Classes -> set Game Viewport Client Class as your customized class.

  3. Project Settings -> Engine -> General Settings -> Default Classes -> set Game Viewport Client Class as your customized class.

  4. Override function InputTouch

     virtual bool InputTouch(FViewport* Viewport, int32 ControllerId, 
     uint32 Handle, ETouchType::Type Type, const FVector2D& TouchLocation, 
     float Force, FDateTime DeviceTimestamp, uint32 TouchpadIndex) override;
    

    Argument TouchLocation is the Touch Position.

Mobile Input - Swipe and Pinch

A plugin for Unreal Engine 4 that exposes touches and swipes on mobile devices as events in blueprints
https://github.com/getsetgames/Swipe

Ultimate Touch Components
https://www.unrealengine.com/marketplace/custom-touch-controls

Controller Rotation

By default, Controller Rotation is equal to PlayerStart Actor Rotation. So if you change the Rotation of PlayerStart Actor, Controller Rotation would be affected.

How to set the rotation of Controller:

void AController::SetControlRotation(const FRotator& NewRotation)
How to create and get Spectator

1st way

Create Spectator:

AMyGameModeBase::AMyGameModeBase()
{
    DefaultPawnClass = ASpectatorPawn::StaticClass();
}

Get Spectator:

ASpectatorPawn* APlayerController::GetSpectatorPawn() const;

2nd way

GetWorld()->SpawnActor<ASpectatorPawn>(ASpectatorPawn::StaticClass());
How to prevent cursor from moving out of window when using dual monitors

Project Settings -> Engine -> Input -> Viewport Properties -> change Default Viewport Mouse Lock Mode to Lock Always.
Then when mouse clicked in viewport, cursor would be locked in viewport.

游戏暂停时是否允许输入执行(默认是禁止输入)

How to disable input when the game is paused
FInputActionBinding& ToggleInGameMenuBinding = InputComponent->BindAction("InGameMenu", IE_Pressed, this, &AStrategyPlayerController::OnToggleInGameMenu);
ToggleInGameMenuBinding.bExecuteWhenPaused = true;

Issues

AddYawInput doesn’t work in Tick of PlayerController

Reason:
It’s strange, maybe a issue of engine?

Solution:
Execute AddControllerYawInput() in ACharacter::Tick() (inherited by AMyCharacter)


三十功名尘与土,八千里路云和月。----岳飞《满江红》