SetWeaponTarget: Difference between revisions
Jump to navigation
Jump to search
(Created page with "__NOTOC__ {{Client function}} Weapon target information: * There are three types of targets: ** Position - Fires at a position in the world ** Entity - Fires at an entity ( eithe...") |
Fernando187 (talk | contribs) (Remove obsolete Requirements section) |
||
(15 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
{{Client function}} | {{Client function}} | ||
Weapon | This function sets the target of a [[Element/Weapon|custom weapon]]. There are 3 different targeting modes, which are explained below. | ||
{{Note|[[Element/Weapon|Custom weapons]] fire targets with no recoil (so they never miss a shot). If you want a custom weapon to take into account recoil, you will have to script it by firing at fixed coordinates.}} | |||
{{OOP|Variable is read only.|[[Element/Weapon|weapon]]:setTarget|target|getWeaponTarget}} | |||
===Required | ==Syntax (target an element)== | ||
Fires the weapon at a physical [[element]]. | |||
<syntaxhighlight lang="lua">bool setWeaponTarget ( weapon theWeapon, element theTarget [, int theComponent = 255 ] )</syntaxhighlight> | |||
===Required arguments=== | |||
* '''theWeapon:''' The weapon to set the target of. | * '''theWeapon:''' The weapon to set the target of. | ||
* '''theTarget:''' The | * '''theTarget:''' The [[element]] to shoot at. It can be a [[player]], [[ped]], [[vehicle]] or [[object]]. | ||
* ''' | |||
** Vehicles: | ===Optional arguments=== | ||
*** 0 | * '''theComponent:''' The component of the target to shoot at. This argument is only relevant when used in the following element types: | ||
*** 1 | ** '''[[Vehicle|Vehicles]]''': | ||
*** 2 | *** '''0''': front left tire. | ||
*** 3 | *** '''1''': front right tire. | ||
*** 255 | *** '''2''': rear left tire. | ||
** Peds: | *** '''3''': rear right tire. | ||
*** | *** '''255''': center of the car (position returned by [[getElementPosition]]). | ||
*** 255 | ** '''[[Ped|Peds]]''' (players '''not''' included; see [[getPedBonePosition]] to know where each bone is located): | ||
***'''1:''' ''BONE_PELVIS1'' position. | |||
***'''2:''' ''BONE_PELVIS'' position. | |||
***'''3:''' ''BONE_SPINE1'' position. | |||
***'''4:''' ''BONE_UPPERTORSO'' position. | |||
***'''5:''' ''BONE_NECK'' position. | |||
***'''6:''' ''BONE_HEAD2'' position. | |||
***'''7:''' ''BONE_HEAD1'' position. | |||
***'''8:''' ''BONE_HEAD'' position. | |||
***'''21:''' ''BONE_RIGHTUPPERTORSO'' position. | |||
***'''22:''' ''BONE_RIGHTSHOULDER'' position. | |||
***'''23:''' ''BONE_RIGHTELBOW'' position. | |||
***'''24:''' ''BONE_RIGHTWRIST'' position. | |||
***'''25:''' ''BONE_RIGHTHAND'' position. | |||
***'''26:''' ''BONE_RIGHTTHUMB'' position. | |||
***'''31:''' ''BONE_LEFTUPPERTORSO'' position. | |||
***'''32:''' ''BONE_LEFTSHOULDER'' position. | |||
***'''33:''' ''BONE_LEFTELBOW'' position. | |||
***'''34:''' ''BONE_LEFTWRIST'' position. | |||
***'''35:''' ''BONE_LEFTHAND'' position. | |||
***'''36:''' ''BONE_LEFTTHUMB'' position. | |||
***'''41:''' ''BONE_LEFTHIP'' position. | |||
***'''42:''' ''BONE_LEFTKNEE'' position. | |||
***'''43:''' ''BONE_LEFTANKLE'' position. | |||
***'''44:''' ''BONE_LEFTFOOT'' position. | |||
***'''51:''' ''BONE_RIGHTHIP'' position. | |||
***'''52:''' ''BONE_RIGHTKNEE'' position. | |||
***'''53:''' ''BONE_RIGHTANKLE'' position. | |||
***'''54:''' ''BONE_RIGHTFOOT'' position. | |||
*** '''255''': center of the ped (position returned by [[getElementPosition]]). | |||
===Returns=== | ===Returns=== | ||
Returns ''true'' on success, ''false'' otherwise. | Returns ''true'' on success, ''false'' otherwise. | ||
==Syntax (target a position)== | |||
Fires the weapon at the specified position. | |||
==Syntax | |||
Fires | |||
<syntaxhighlight lang="lua">bool setWeaponTarget ( weapon theWeapon, float targetX, float targetY, float targetZ )</syntaxhighlight> | <syntaxhighlight lang="lua">bool setWeaponTarget ( weapon theWeapon, float targetX, float targetY, float targetZ )</syntaxhighlight> | ||
===Required | ===Required arguments=== | ||
* '''theWeapon:''' The weapon to set the target of. | * '''theWeapon:''' The weapon to set the target of. | ||
* '''targetX:''' The target X. | * '''targetX:''' The target X. | ||
Line 43: | Line 71: | ||
Returns ''true'' on success, ''false'' otherwise. | Returns ''true'' on success, ''false'' otherwise. | ||
==Syntax | ==Syntax (rotational target)== | ||
Sets the weapon back to rotation based targeting. | Sets the weapon back to rotation based targeting. It will fire to its front. | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua">bool setWeaponTarget ( weapon theWeapon, nil )</syntaxhighlight> | ||
===Required | ===Required arguments=== | ||
* '''theWeapon:''' The weapon to clear the target of. | * '''theWeapon:''' The weapon to clear the target of. | ||
Line 54: | Line 82: | ||
Returns ''true'' on success, ''false'' otherwise. | Returns ''true'' on success, ''false'' otherwise. | ||
==Example== | |||
This example creates a Uzi and a pedestrian in the center of the map when the resource which contains it starts and makes the weapon fire the new ped's head non-stop. | |||
<syntaxhighlight lang="lua">local function createWeaponFiringPedHead() | |||
-- Create the weapon and set a flag of it | |||
local weapon = createWeapon("uzi", 0, 0, 10) | |||
setWeaponFlags(weapon, "instant_reload", true) | |||
-- Set the weapon target to a ped and fire it forever | |||
setWeaponTarget(weapon, createPed(8, 0, 0, 5), 8) | |||
setWeaponState(weapon, "firing") | |||
end | |||
addEventHandler("onClientResourceStart", resourceRoot, createWeaponFiringPedHead)</syntaxhighlight> | |||
==See also== | |||
{{Client weapon creation functions}} | |||
==See | |||
{{Client weapon functions}} |
Latest revision as of 17:06, 7 November 2024
This function sets the target of a custom weapon. There are 3 different targeting modes, which are explained below.
OOP Syntax Help! I don't understand this!
- Note: Variable is read only.
- Method: weapon:setTarget(...)
- Variable: .target
- Counterpart: getWeaponTarget
Syntax (target an element)
Fires the weapon at a physical element.
bool setWeaponTarget ( weapon theWeapon, element theTarget [, int theComponent = 255 ] )
Required arguments
- theWeapon: The weapon to set the target of.
- theTarget: The element to shoot at. It can be a player, ped, vehicle or object.
Optional arguments
- theComponent: The component of the target to shoot at. This argument is only relevant when used in the following element types:
- Vehicles:
- 0: front left tire.
- 1: front right tire.
- 2: rear left tire.
- 3: rear right tire.
- 255: center of the car (position returned by getElementPosition).
- Peds (players not included; see getPedBonePosition to know where each bone is located):
- 1: BONE_PELVIS1 position.
- 2: BONE_PELVIS position.
- 3: BONE_SPINE1 position.
- 4: BONE_UPPERTORSO position.
- 5: BONE_NECK position.
- 6: BONE_HEAD2 position.
- 7: BONE_HEAD1 position.
- 8: BONE_HEAD position.
- 21: BONE_RIGHTUPPERTORSO position.
- 22: BONE_RIGHTSHOULDER position.
- 23: BONE_RIGHTELBOW position.
- 24: BONE_RIGHTWRIST position.
- 25: BONE_RIGHTHAND position.
- 26: BONE_RIGHTTHUMB position.
- 31: BONE_LEFTUPPERTORSO position.
- 32: BONE_LEFTSHOULDER position.
- 33: BONE_LEFTELBOW position.
- 34: BONE_LEFTWRIST position.
- 35: BONE_LEFTHAND position.
- 36: BONE_LEFTTHUMB position.
- 41: BONE_LEFTHIP position.
- 42: BONE_LEFTKNEE position.
- 43: BONE_LEFTANKLE position.
- 44: BONE_LEFTFOOT position.
- 51: BONE_RIGHTHIP position.
- 52: BONE_RIGHTKNEE position.
- 53: BONE_RIGHTANKLE position.
- 54: BONE_RIGHTFOOT position.
- 255: center of the ped (position returned by getElementPosition).
- Vehicles:
Returns
Returns true on success, false otherwise.
Syntax (target a position)
Fires the weapon at the specified position.
bool setWeaponTarget ( weapon theWeapon, float targetX, float targetY, float targetZ )
Required arguments
- theWeapon: The weapon to set the target of.
- targetX: The target X.
- targetY: The target Y.
- targetZ: The target Z.
Returns
Returns true on success, false otherwise.
Syntax (rotational target)
Sets the weapon back to rotation based targeting. It will fire to its front.
bool setWeaponTarget ( weapon theWeapon, nil )
Required arguments
- theWeapon: The weapon to clear the target of.
Returns
Returns true on success, false otherwise.
Example
This example creates a Uzi and a pedestrian in the center of the map when the resource which contains it starts and makes the weapon fire the new ped's head non-stop.
local function createWeaponFiringPedHead() -- Create the weapon and set a flag of it local weapon = createWeapon("uzi", 0, 0, 10) setWeaponFlags(weapon, "instant_reload", true) -- Set the weapon target to a ped and fire it forever setWeaponTarget(weapon, createPed(8, 0, 0, 5), 8) setWeaponState(weapon, "firing") end addEventHandler("onClientResourceStart", resourceRoot, createWeaponFiringPedHead)
See also
- createWeapon
- fireWeapon
- getWeaponAmmo
- getWeaponClipAmmo
- getWeaponFiringRate
- getWeaponFlags
- getWeaponOwner
- getWeaponProperty
- getWeaponState
- getWeaponTarget
- resetWeaponFiringRate
- setWeaponAmmo
- setWeaponClipAmmo
- setWeaponFiringRate
- setWeaponFlags
- setWeaponProperty
- setWeaponState
- setWeaponTarget