setCameraTarget | Multi Theft Auto: Wiki Skip to content

setCameraTarget

Client-side
Server-side
Shared

Pair: getCameraTarget

This function allows you to set a player's camera to follow other elements instead. Currently supported element type is player, ped and vehicle.

OOP Syntax Help! I don't understand this!

Server OOP syntax
  • Method:player:setCameraTarget(...)
  • Variable: .target
Client OOP syntax
  • Method:Camera.setTarget(...)
  • Variable: .target

Client Syntax

bool setCameraTarget ( [ element target = nil ] )
Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use.

  • target (default: nil): The element who you want the camera to follow. If none is specified, the camera will target the player.

Returns

  • bool: result

Returns true if the function was successful, false otherwise.

Second Client Syntax

bool setCameraTarget ( float targetX, float targetY, float targetZ )
Required Arguments
  • targetX: The target X position that you want the local camera to look at.
  • targetY: The target Y position that you want the local camera to look at.
  • targetZ: The target Z position that you want the local camera to look at.

Returns

  • bool: result

Returns true if the function was successful, false otherwise.

Server Syntax

bool setCameraTarget ( player thePlayer, [ element target = nil ] )
Required Arguments
  • thePlayer: The player whose camera you wish to modify.
Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use.

  • target (default: nil): The element who you want the camera to follow. If none is specified, the camera will target the player.

Returns

  • bool: result

Returns true if the function was successful, false otherwise.

Code Examples

client

This is an example of how one could implement a spectator function. Using the left and right arrow keys you can view other players. Note that this code isn't complete as it doesn't take into account joining or quitting players.

g_Players = getElementsByType("player") -- get a list of all players in the server
for i, aPlayer in ipairs(g_Players) do -- find out what index the local player has in the list
if aPlayer == localPlayer then
g_CurrentSpectated = i
break
end
end
function spectatePrevious() -- decrement the spectate index and spectate the corresponding player
if g_CurrentSpectated == 1 then
g_CurrentSpectated = #g_Players
else
g_CurrentSpectated = g_CurrentSpectated - 1
end
setCameraTarget(g_Players[g_CurrentSpectated])
end
function spectateNext() -- increment the spectate index and spectate the corresponding player
if g_CurrentSpectated == #g_Players then
g_CurrentSpectated = 1
else
g_CurrentSpectated = g_CurrentSpectated + 1
end
setCameraTarget(g_Players[g_CurrentSpectated])
end
-- Bind above functions to arrow keys
bindKey("arrow_l", "down", spectatePrevious)
bindKey("arrow_r", "down", spectateNext)

Changelog

  • 1.5.8-9.20683

    Added support for vehicle and ped types.