GetPedHitBone: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "{{Useful Function}} <lowercasetitle></lowercasetitle> __NOTOC__ This function gets the approximate number of the bone where the ped is hit ==Syntax== <syntaxhighlight lang="lua"> int getPedHitBone( ped thePed, float hitX, float hitY, float hitZ ) </syntaxhighlight> ===Required Arguments=== * '''thePed:''' the ped whose bone you want to get. *'''hitX''', '''hitY''', '''hitZ''': float world coordinates representing a hit point. ===Returns=== An '''int''' with the...")
 
No edit summary
 
(One intermediate revision by the same user not shown)
Line 21: Line 21:
   assert(tonumber(hitY), "Bad argument @ 'getPedHitBone' [Expected vector3 at argument 3, got " .. tostring(hitY) .. "]")
   assert(tonumber(hitY), "Bad argument @ 'getPedHitBone' [Expected vector3 at argument 3, got " .. tostring(hitY) .. "]")
   assert(tonumber(hitZ), "Bad argument @ 'getPedHitBone' [Expected vector3 at argument 4, got " .. tostring(hitZ) .. "]")
   assert(tonumber(hitZ), "Bad argument @ 'getPedHitBone' [Expected vector3 at argument 4, got " .. tostring(hitZ) .. "]")
   local nowDistance
   local nowDistance, hitBone
   local boneIDs = {0, 1, 2, 3, 4, 5, 6, 7, 8, 21, 22, 23, 24, 25, 26, 31, 32, 33, 34, 35, 36, 41, 42, 43, 44, 51, 52, 53, 54}
   local boneIDs = {0, 1, 2, 3, 4, 5, 6, 7, 8, 21, 22, 23, 24, 25, 26, 31, 32, 33, 34, 35, 36, 41, 42, 43, 44, 51, 52, 53, 54}
   for _,bone in pairs(boneIDs) do
   for _,bone in pairs(boneIDs) do
Line 45: Line 45:
   assert(tonumber(hitY), "Bad argument @ 'getPedHitBone' [Expected vector3 at argument 3, got " .. tostring(hitY) .. "]")
   assert(tonumber(hitY), "Bad argument @ 'getPedHitBone' [Expected vector3 at argument 3, got " .. tostring(hitY) .. "]")
   assert(tonumber(hitZ), "Bad argument @ 'getPedHitBone' [Expected vector3 at argument 4, got " .. tostring(hitZ) .. "]")
   assert(tonumber(hitZ), "Bad argument @ 'getPedHitBone' [Expected vector3 at argument 4, got " .. tostring(hitZ) .. "]")
   local nowDistance
   local nowDistance, hitBone
   local boneIDs = {0, 1, 2, 3, 4, 5, 6, 7, 8, 21, 22, 23, 24, 25, 26, 31, 32, 33, 34, 35, 36, 41, 42, 43, 44, 51, 52, 53, 54}
   local boneIDs = {0, 1, 2, 3, 4, 5, 6, 7, 8, 21, 22, 23, 24, 25, 26, 31, 32, 33, 34, 35, 36, 41, 42, 43, 44, 51, 52, 53, 54}
   for _,bone in pairs(boneIDs) do
   for _,bone in pairs(boneIDs) do
Line 66: Line 66:
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
'''Author:''' Rick

Latest revision as of 23:30, 7 May 2022

This function gets the approximate number of the bone where the ped is hit

Syntax

int getPedHitBone( ped thePed, float hitX, float hitY, float hitZ )

Required Arguments

  • thePed: the ped whose bone you want to get.
  • hitX, hitY, hitZ: float world coordinates representing a hit point.

Returns

An int with the approximate bone where the ped take damage, does not work properly serverside

Code

Click to collapse [-]
Function source
function getPedHitBone(thePed, hitX, hitY, hitZ)
   assert(isElement(thePed) and (getElementType(thePed) == "ped" or getElementType(thePed) == "player"), "Bad argument @ 'getPedHitBone' [Expected ped/player at argument 1, got " .. tostring(thePed) .. "]")
   assert(tonumber(hitX), "Bad argument @ 'getPedHitBone' [Expected vector3 at argument 2, got " .. tostring(hitX) .. "]")
   assert(tonumber(hitY), "Bad argument @ 'getPedHitBone' [Expected vector3 at argument 3, got " .. tostring(hitY) .. "]")
   assert(tonumber(hitZ), "Bad argument @ 'getPedHitBone' [Expected vector3 at argument 4, got " .. tostring(hitZ) .. "]")
   local nowDistance, hitBone
   local boneIDs = {0, 1, 2, 3, 4, 5, 6, 7, 8, 21, 22, 23, 24, 25, 26, 31, 32, 33, 34, 35, 36, 41, 42, 43, 44, 51, 52, 53, 54}
   for _,bone in pairs(boneIDs) do
      local boneX, boneY, boneZ = getPedBonePosition(thePed, bone)
      local distance = getDistanceBetweenPoints3D(hitX, hitY, hitZ, boneX, boneY, boneZ)
      if nowDistance and (distance < nowDistance) then
         nowDistance, hitBone = distance, bone
      elseif not nowDistance then
         nowDistance, hitBone = distance, bone
      end
   end
   return hitBone
end

Example

Click to collapse [-]
Client
function getPedHitBone(thePed, hitX, hitY, hitZ)
   assert(isElement(thePed) and (getElementType(thePed) == "ped" or getElementType(thePed) == "player"), "Bad argument @ 'getPedHitBone' [Expected ped/player at argument 1, got " .. tostring(thePed) .. "]")
   assert(tonumber(hitX), "Bad argument @ 'getPedHitBone' [Expected vector3 at argument 2, got " .. tostring(hitX) .. "]")
   assert(tonumber(hitY), "Bad argument @ 'getPedHitBone' [Expected vector3 at argument 3, got " .. tostring(hitY) .. "]")
   assert(tonumber(hitZ), "Bad argument @ 'getPedHitBone' [Expected vector3 at argument 4, got " .. tostring(hitZ) .. "]")
   local nowDistance, hitBone
   local boneIDs = {0, 1, 2, 3, 4, 5, 6, 7, 8, 21, 22, 23, 24, 25, 26, 31, 32, 33, 34, 35, 36, 41, 42, 43, 44, 51, 52, 53, 54}
   for _,bone in pairs(boneIDs) do
      local boneX, boneY, boneZ = getPedBonePosition(thePed, bone)
      local distance = getDistanceBetweenPoints3D(hitX, hitY, hitZ, boneX, boneY, boneZ)
      if nowDistance and (distance < nowDistance) then
         nowDistance, hitBone = distance, bone
      elseif not nowDistance then
         nowDistance, hitBone = distance, bone
      end
   end
   return hitBone
end

addEventHandler( "onClientPlayerWeaponFire", root, function(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement, startX, startY, startZ)
   if isElement(hitElement) then
      outputChatBox("You hit bone "..getPedHitBone(hitElement, hitX, hitY, hitZ) )
   end
end)

Author: Rick