Controller
/// info
A PlayerController is automatically created for each player that joins the game.
It serves as the player's presence in the world, handling input and controlling a pawn when one is possessed.
///
/// tip
PlayerController is an Actor so it inherits all functions from Actor
///
local Controller = UE.UGameplayStatics.GetPlayerController(HWorld, 0)
Functions
Possess
Handles attaching this controller to the specified pawn Only runs on the network authority (where HasAuthority() returns true)
Controller:Possess(pawn)
UnPossess
Called to unpossess our pawn for any reason that is not the pawn being destroyed
Controller:UnPossess()
K2_GetPawn
Return the Pawn that is currently 'controlled' by this PlayerController
Controller:K2_GetPawn()
StopMovement
Aborts the move the controller is currently performing
Controller:StopMovement()
SetIgnoreMoveInput
Locks or unlocks movement input
Controller:SetIgnoreMoveInput(true)
SetIgnoreLookInput
Locks or unlocks look input
Controller:SetIgnoreLookInput(true)
IsMoveInputIgnored
Returns true if movement input is ignored
Controller:IsMoveInputIgnored()
IsLookInputIgnored
Returns true if look input is ignored
Controller:IsLookInputIgnored()
ResetIgnoreMoveInput
Stops ignoring move input by resetting the ignore move input state
Controller:ResetIgnoreMoveInput()
ResetIgnoreLookInput
Stops ignoring look input by resetting the ignore look input state
Controller:ResetIgnoreLookInput()
GetViewTarget
Get the actor the controller is looking at
Controller:GetViewTarget()
WasInputKeyJustReleased
Returns true if the given key/button was down last frame and up this frame
local key = UE.FKey("E")
Controller:WasInputKeyJustReleased(key)
WasInputKeyJustPressed
Returns true if the given key/button was up last frame and down this frame
local key = UE.FKey("E")
Controller:WasInputKeyJustPressed(key)
SetName
Tries to set the player's name to the given name
local name = 'Kakarot'
Controller:SetName(name)
SetMouseLocation
Positions the mouse cursor in screen space, in pixels
local x, y = 100, 100
Controller:SetMouseLocation(x, y)
GetMousePosition
Retrieves the current X and Y screen coordinates of the mouse cursor.
Returns false if no mouse is available (e.g. on gamepad or touchscreen).
local X, Y = Controller:GetMousePosition(X, Y)
print("Mouse is at:", X, Y)
IsInputKeyDown
Returns true if the given key/button is pressed on the input of the controller (if present)
local key = UE.FKey("E")
Controller:IsInputKeyDown(key)
GetInputKeyTimeDown
Returns how long the given key/button has been down. Returns 0 if it's up or it just went down this frame
local key = UE.FKey("E")
Controller:IsInputKeyDown(key)
GetViewportSize
Returns the current size of the viewport (screen resolution in pixels) for this player controller
local SizeX, SizeY = Controller:GetViewportSize(SizeX, SizeY)
print("Viewport size:", SizeX, SizeY)
GetInputMouseDelta
Retrieves how far the mouse moved this frame on the X and Y axes
local DeltaX, DeltaY = Controller:GetInputMouseDelta(DeltaX, DeltaY)
print("Mouse moved:", DeltaX, DeltaY)
FOV
Set the field of view
Controller:FOV(100)
ProjectWorldLocationToScreen
Convert a World Space 3D position into a 2D Screen Space position
local worldPos = Vector(100, 200, 300)
local screenPos = Vector2D()
Controller:ProjectWorldLocationToScreen(worldPos, screenPos, false)
print("Screen position:", screenPos.X, screenPos.Y)
DeprojectScreenPositionToWorld
Convert 2D screen position to World Space 3D position and direction Returns false if unable to determine value
local ScreenPos = Vector2D(960, 540)
local WorldLoc = Vector(0, 0, 0)
local WorldDir = Vector(0, 0, 0)
Controller:DeprojectScreenPositionToWorld(
ScreenPos.X,
ScreenPos.Y,
WorldLoc,
WorldDir
)
print("World location:", WorldLoc)
print("World direction:", WorldDir)
DeprojectMousePositionToWorld
Converts the current mouse 2D position into a 3D world location and direction
Returns false if unable to deproject (e.g., no mouse available)
local WorldLocation = Vector(0, 0, 0)
local WorldDirection = Vector(0, 0, 0)
Controller:DeprojectMousePositionToWorld(WorldLocation, WorldDirection)
print("Ray origin:", WorldLocation)
print("Ray direction:", WorldDirection)
ClientSetViewTarget
Sets the view target (camera focus) of the player to the specified actor You can use this to smoothly switch to another camera, focus on a cinematic actor, or temporarily follow another pawn. To make an instant switch, set BlendTime to 0
local target = MyCameraActor -- any AActor, e.g., ACameraActor, Pawn, etc.
local params = UE.FViewTargetTransitionParams()
params.BlendTime = 1.5
params.BlendFunction = UE.EViewTargetBlendFunction.VTBlend_Cubic
Controller:ClientSetViewTarget(target, params)
ClientPlaySound
Plays a sound locally on the client (only this player will hear it)
local sound = UE.UObject.Load("/Game/Sounds/Click.Click")
Controller:ClientPlaySound(sound, 1.0, 1.0)
AddYawInput
Adds horizontal rotation input (turning left/right).
This value is scaled by the controller's InputYawScale
Controller:AddYawInput(2.0) -- Turn right slightly
AddRollInput
Adds roll rotation input (tilting the camera left/right). This value is scaled by the controller's InputRollScale
Controller:AddRollInput(1.0) -- Tilt right slightly
AddPitchInput
Adds vertical rotation input (looking up/down). This value is scaled by the controller's InputPitchScale
Controller:AddPitchInput(-1.0) -- Look down slightly