SetPedLookAt: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(Add example for remote player usage)
 
(17 intermediate revisions by 13 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
{{Client function}}
{{Client function}}
{{Needs Example}}
{{Note|Avoid calling setPedLookAt every frame as this can cause bugs like being invincible to burning.}}
{{Important Note|For remote players, you have to use [https://wiki.multitheftauto.com/wiki/SetPedAimTarget setPedAimTarget] before setPedLookAt.}}
Makes a ped turn his head and look at a specific world position or element.
Makes a ped turn his head and look at a specific world position or element.
==Syntax==
==Syntax==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
bool setPedLookAt ( ped thePed, float x, float y, float z [, int time = 3000, int blend = 1000, element target ] )
bool setPedLookAt ( ped thePed, float x, float y, float z [, int time = 3000 [, int blend = 1000 ], element target = nil ] )
</syntaxhighlight>
</syntaxhighlight>


Line 18: Line 18:
*'''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.
*'''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.
*'''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.
*'''target:''' if this argument is specified, the position arguments will be mean offsets relative to the target and the ped's gaze will follow the specified element instead. Can be a player, a vehicle, another ped etc.


==Example==
==Example==
This example makes player to always see where the camera points at.
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 screenSize_X, screenSize_Y = guiGetScreenSize()
    local rotcam = math.rad (360 - getPedCameraRotation (localPlayer))
 
    local xpos,ypos,zpos = getPedBonePosition (localPlayer, 8)
function pedLookAt()
    local xlook,ylook,zlook = xpos - 300*math.sin(rotcam), ypos + 300*math.cos(rotcam), zpos
  local x, y, z = getWorldFromScreenPosition(screenSize_X / 2, screenSize_Y / 2, 15)
    setPedLookAt (localPlayer, xlook, ylook, zlook, 1500, 2000)
  setPedLookAt(localPlayer, x, y, z, -1, 0)
end
setTimer(pedLookAt, 120, 0)
</syntaxhighlight>
 
This example makes remote players heads move based on what direction their camera is facing.
 
<syntaxhighlight lang="lua">function remotePlayerHeadMoving()
local x, y, z = getElementPosition(localPlayer)
for i, player in pairs(getElementsWithinRange(x, y, z, 35, "player")) do
if (player ~= localPlayer and isElementOnScreen(player)) then
local rot = getPedCameraRotation(player)
local x, y, z = getElementPosition(player)
local vx = x + math.sin(math.rad(rot)) * 10
local vy = y + math.cos(math.rad(rot)) * 10
-- To fix remote player's head bug + check to avoid setPedAimTarget movement lag while aiming with gun
if (getPedTask(player, "secondary", 0) ~= "TASK_SIMPLE_USE_GUN") then
if (player ~= localPlayer) then
setPedAimTarget(player, vx, vy, z) -- head bug fix
end
setPedLookAt(player, vx, vy, z, -1, 0)
end
end
    end
end
end
addEventHandler ("onClientRender", root, onClientLookAtRender)
setTimer(remotePlayerHeadMoving, 100, 0)
</syntaxhighlight>
</syntaxhighlight>


== Issues ==
{{Issues|
{{Issue|509|setPedLookAt does not work for remote players}}
{{Issue|626|setPedLookAt cancels damage done by any sort of fire}}
}}
==See Also==
==See Also==
{{Client ped functions}}
{{Client ped functions}}

Latest revision as of 09:38, 7 March 2026

[[{{{image}}}|link=|]] Note: Avoid calling setPedLookAt every frame as this can cause bugs like being invincible to burning.
[[{{{image}}}|link=|]] Important Note: For remote players, you have to use setPedAimTarget before setPedLookAt.

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 mean offsets relative to the target and the ped's gaze will follow the specified element instead. Can be a player, a vehicle, another ped etc.

Example

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 screenSize_X, screenSize_Y = guiGetScreenSize()

function pedLookAt()
   local x, y, z = getWorldFromScreenPosition(screenSize_X / 2, screenSize_Y / 2, 15)
   setPedLookAt(localPlayer, x, y, z, -1, 0)
end
setTimer(pedLookAt, 120, 0)

This example makes remote players heads move based on what direction their camera is facing.

function remotePlayerHeadMoving()
	local x, y, z = getElementPosition(localPlayer)
	for i, player in pairs(getElementsWithinRange(x, y, z, 35, "player")) do
		if (player ~= localPlayer and isElementOnScreen(player)) then
			local rot = getPedCameraRotation(player)
			local x, y, z = getElementPosition(player)
			local vx = x + math.sin(math.rad(rot)) * 10
			local vy = y + math.cos(math.rad(rot)) * 10
			-- To fix remote player's head bug + check to avoid setPedAimTarget movement lag while aiming with gun
			if (getPedTask(player, "secondary", 0) ~= "TASK_SIMPLE_USE_GUN") then
				if (player ~= localPlayer) then
					setPedAimTarget(player, vx, vy, z) -- head bug fix
				end
				setPedLookAt(player, vx, vy, z, -1, 0)
			end
		end
    end
end
setTimer(remotePlayerHeadMoving, 100, 0)

Issues

Issue ID Description
#509 setPedLookAt does not work for remote players
#626 setPedLookAt cancels damage done by any sort of fire

See Also