SetPedAimTarget: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
m (→‎Example: fixed a typo)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
{{Client function}}
{{Client function}}
This function allows you to set a ped's aim target to a specific point.
This function allows you to set a ped's aim target to a specific point. If a ped is within a certain range defined by [[getPedTargetStart]] and [[getPedTargetEnd]] he will be targeted and shot.
 
''Note: If you wish to make a ped shoot you must use this in conjunction with an equipped weapon and [[setPedControlState]].''


==Syntax==  
==Syntax==  
Line 18: Line 20:


==Example==
==Example==
'''Example 1:''' This example creates a ped in the middle of the map and sets its aim target to point north-east.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function createPedAndsetHisAimTarget ()
function createPedAndsetHisAimTarget ()
Line 23: Line 26:
         setPedAimTarget ( ped, 10, 10, 5 ) -- set the ped's target to a point in North-East
         setPedAimTarget ( ped, 10, 10, 5 ) -- set the ped's target to a point in North-East
end
end
</syntaxhighlight>
'''Example 2:''' This example creates a ped at grove street that will shoot at anything that enters a certain marker.
<syntaxhighlight lang="lua">
local cj = createPed(0, 2498.5, -1684.0, 13.5, 20) -- create a ped at cjs house in grove street and give it an ak
givePedWeapon(cj, 30, 3000, true)
local marker = createMarker(2493.0, -1669.0, 13.5, "checkpoint", 3, 255, 0, 0, 128) -- create a marker and get its associated colshape for later use
local colshape = getElementColShape(marker)
function renderHandler()
local intruder = getElementsWithinColShape(colshape)[2] -- get the second element found within the colshape, because the first one will normally be the marker itself
if intruder then
local x,y,z = getElementPosition(intruder) -- if an intruder exists, get its position and have the cj ped fire at it
setPedAimTarget(cj, x, y, z)
setPedControlState(cj, "fire", true)
else
setPedControlState(cj, "fire", false) -- otherwise stop shooting
end
end
addEventHandler("onClientRender", getRootElement(), renderHandler) -- add an event handler to go through the target acquisition procedure every frame
</syntaxhighlight>
</syntaxhighlight>


==See Also==
==See Also==
{{Client ped functions}}
{{Client ped functions}}

Latest revision as of 14:26, 21 September 2023

This function allows you to set a ped's aim target to a specific point. If a ped is within a certain range defined by getPedTargetStart and getPedTargetEnd he will be targeted and shot.

Note: If you wish to make a ped shoot you must use this in conjunction with an equipped weapon and setPedControlState.

Syntax

bool setPedAimTarget ( ped thePed, float x, float y, float z )

Required Arguments

  • thePed: The ped whose target you want to set. Only peds and remote players will work; this function has no effect on the local player.
  • x: The x coordinate of the aim target point.
  • y: The y coordinate of the aim target point.
  • z: The z coordinate of the aim target point.

Returns

Returns true if the function was successful, false otherwise.

Example

Example 1: This example creates a ped in the middle of the map and sets its aim target to point north-east.

function createPedAndsetHisAimTarget ()
        local ped = createPed (0, 0, 0, 5 ) -- create a ped, who looks like cj, in the middle of the map
        setPedAimTarget ( ped, 10, 10, 5 ) -- set the ped's target to a point in North-East
end

Example 2: This example creates a ped at grove street that will shoot at anything that enters a certain marker.

local cj = createPed(0, 2498.5, -1684.0, 13.5, 20) -- create a ped at cjs house in grove street and give it an ak
givePedWeapon(cj, 30, 3000, true)

local marker = createMarker(2493.0, -1669.0, 13.5, "checkpoint", 3, 255, 0, 0, 128) -- create a marker and get its associated colshape for later use
local colshape = getElementColShape(marker)

function renderHandler()
	local intruder = getElementsWithinColShape(colshape)[2] -- get the second element found within the colshape, because the first one will normally be the marker itself
	if intruder then
		local x,y,z = getElementPosition(intruder) -- if an intruder exists, get its position and have the cj ped fire at it
		setPedAimTarget(cj, x, y, z)
		setPedControlState(cj, "fire", true)
	else
		setPedControlState(cj, "fire", false) -- otherwise stop shooting
	end
end
addEventHandler("onClientRender", getRootElement(), renderHandler) -- add an event handler to go through the target acquisition procedure every frame

See Also