GetPedHitBone

From Multi Theft Auto: Wiki
Revision as of 23:30, 7 May 2022 by GalAnonim (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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