SetCameraTarget: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Wrong code for placing the camera in the front)
m (Added changelog)
 
(20 intermediate revisions by 16 users not shown)
Line 1: Line 1:
{{Needs_Checking|The function already places camera behind the player. No second call or a timer is needed.}}
__NOTOC__
__NOTOC__
{{Server client function}}
{{Server client function}}
This function allows you to set a player's camera to follow other elements instead. Currently supported element type is:
This function allows you to set a player's camera to follow other elements instead. Currently supported element type is:
*[[Player]]s
*[[Player]]s
*[[Ped]]s
*[[Vehicle]]s


==Syntax==
==Syntax==
<section name="Server" class="server" show="true">
<section name="Server" class="server" show="true">
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
bool setCameraTarget ( player thePlayer, [ element target ] )
bool setCameraTarget ( player thePlayer [, element target = nil ] )
</syntaxhighlight>
</syntaxhighlight>
 
{{OOP||[[player]]:setCameraTarget|cameraTarget|getCameraTarget}}
===Required Arguments===  
===Required Arguments===  
*'''thePlayer:''' The player whose camera you wish to modify.
*'''thePlayer:''' The player whose camera you wish to modify.


===Optional Arguments===
===Optional Arguments===
*'''target:''' The element that you want the camera to follow. If none is specified, the camera will target the player.
*'''target:''' The element who you want the camera to follow. If none is specified, the camera will target the player.
</section>
</section>


<section name="Client" class="client" show="true">
<section name="Client 1" class="client" show="true">
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
bool setCameraTarget ( element target )
bool setCameraTarget ( element target )
Line 25: Line 25:


===Required Arguments===  
===Required Arguments===  
*'''target:''' The element that you want the local camera to follow.
*'''target:''' The element who you want the local camera to follow.
</section>
</section>
{{New feature/item|3.0132|1.3.1|5212|
<section name="Client 2" class="client" show="true">
This syntax mantains the player targeted by the camera, but makes the camera look at the specified coordinates. It has no effect when the camera doesn't have a target.
<syntaxhighlight lang="lua">bool setCameraTarget ( float targetX, float targetY, float targetZ )</syntaxhighlight>
===Required Arguments===
*'''targetX, targetY, targetZ:''' The target position that you want the local camera to look at.
</section>}}


===Returns===
===Returns===
Line 37: Line 46:
g_Players = getElementsByType("player")        -- get a list of all players in the server
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
for i,aPlayer in ipairs(g_Players) do          -- find out what index the local player has in the list
     if aPlayer == getLocalPlayer() then
     if aPlayer == localPlayer then
         g_CurrentSpectated = i
         g_CurrentSpectated = i
         break
         break
Line 66: Line 75:
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
==Changelog==
{{ChangelogHeader}}
{{ChangelogItem|1.5.8-9.20683|Added support for ''vehicle'' and ''ped'' types}}


==See Also==
==See Also==
{{Camera functions}}
{{Camera functions}}
[[hu:setCameraTarget]]
[[RO:setCameraTarget]]

Latest revision as of 18:03, 12 March 2024

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

Syntax

Click to collapse [-]
Server
bool setCameraTarget ( player thePlayer [, element target = nil ] )

OOP Syntax Help! I don't understand this!

Method: player:setCameraTarget(...)
Variable: .cameraTarget
Counterpart: getCameraTarget


Required Arguments

  • thePlayer: The player whose camera you wish to modify.

Optional Arguments

  • target: The element who you want the camera to follow. If none is specified, the camera will target the player.
Click to collapse [-]
Client 1
bool setCameraTarget ( element target )

Required Arguments

  • target: The element who you want the local camera to follow.
Click to collapse [-]
Client 2

This syntax mantains the player targeted by the camera, but makes the camera look at the specified coordinates. It has no effect when the camera doesn't have a target.

bool setCameraTarget ( float targetX, float targetY, float targetZ )

Required Arguments

  • targetX, targetY, targetZ: The target position that you want the local camera to look at.

Returns

Returns true if the function was successful, false otherwise.

Example

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.

Click to collapse [-]
Client script
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

Version Description
1.5.8-9.20683 Added support for vehicle and ped types

See Also