SetPedLookAt: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (Expanding of the example)
(Made →‎Example: simpler and more accurate when looking up or down)
Line 20: Line 20:


==Example==
==Example==
This example makes player to always see where the camera points at.
<section name="Client" class="client" show="true">
<section name="Client" class="client" show="true">
This example makes the local player look at where the camera points at. If you want to sync this effect with other players you can use [[triggerLatentServerEvent]] and [[triggerLatentClientEvent]] functions.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function onClientLookAtRender()
local sx, sy = guiGetScreenSize()
     for _, player in ipairs ( getElementsByType ( "player", root, true ) ) do
 
        local rotcam = math.rad ( 360 - getPedCameraRotation ( player ) )
function updateLookAt()
        local xpos, ypos, zpos = getPedBonePosition ( player, 8 )
     -- The world coordinates of the center of the screen always are the point where the camera really looks at, so get them.
        local xlook, ylook, zlook = xpos - 300 * math.sin( rotcam ), ypos + 300 * math.cos( rotcam ), zpos
    local tx, ty, tz = getWorldFromScreenPosition(sx / 2, sy / 2, 10)
        setPedLookAt ( player, xlook, ylook, zlook )
    setPedLookAt(localPlayer, tx, ty, tz, -1, 0) -- Make the player look that coordinates
    end
end
end
addEventHandler ( "onClientRender", root, onClientLookAtRender )
addEventHandler("onClientPreRender", root, updateLookAt)
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>

Revision as of 16:38, 19 August 2014

Makes a ped turn his head and look at a specific world position or element.

Syntax

bool setPedLookAt ( ped thePed, float x, float y, float z [, int time = 3000 [, int blend = 1000 ], element target = nil ] )

Required Arguments

  • thePed: the ped to change the lookat of.
  • x: the x coordinate of the world position to look at.
  • y: the y coordinate of the world position to look at.
  • z: the z coordinate of the world position to look at.

Optional Arguments

  • time: the time, in milliseconds, during which the ped will look at the target. Once this time has elapsed, he will look ahead again like before the function was applied. A time of 0 will immediately stop any lookat. A negative time will make the ped look at the target indefinitely.
  • blend: the time, in milliseconds, during which the look will blend.
  • target: if this argument is specified, the position arguments will be ignored and the ped's gaze will follow the specified element instead. Can be a player, a vehicle, another ped etc.

Example

Click to collapse [-]
Client

This example makes the local player look at where the camera points at. If you want to sync this effect with other players you can use triggerLatentServerEvent and triggerLatentClientEvent functions.

local sx, sy = guiGetScreenSize()

function updateLookAt()
    -- The world coordinates of the center of the screen always are the point where the camera really looks at, so get them.
    local tx, ty, tz = getWorldFromScreenPosition(sx / 2, sy / 2, 10)
    setPedLookAt(localPlayer, tx, ty, tz, -1, 0) -- Make the player look that coordinates
end
addEventHandler("onClientPreRender", root, updateLookAt)

See Also