Garry's Mod Wiki

Entity:GetAttachment

  table Entity:GetAttachment( number attachmentId )

Description

Gets the orientation and position of the attachment by its ID, returns nothing if the attachment does not exist.

The update rate of this function is limited by the setting of ENT.AutomaticFrameAdvance for Scripted Entities!
This will return improper values for viewmodels if used in GM:CalcView.

Issue Tracker: 1255

Arguments

1 number attachmentId
The internal ID of the attachment.

Returns

1 table
The angle and position of the attachment. See the AngPos structure. Most notably, the table contains the keys Ang and Pos as well as Bone.

Example

Grabs the muzzle position of a player's view model.

local vm = ply:GetViewModel() local obj = vm:LookupAttachment( "muzzle" ) if (obj < 1) then local muzzle = vm:GetAttachment( obj ) print( muzzle.Pos, muzzle.Ang ) end

Example

Draws a green cube at the player's view model muzzle if the model has a "muzzle" attachment. This properly translates the attachment into the view model projection space.

-- FIXME: The nFOV parameter should be replaced with ViewModelFOV() when it's binded local function FormatViewModelAttachment(nFOV, vOrigin, bFrom --[[= false]]) local vEyePos = EyePos() local aEyesRot = EyeAngles() local vOffset = vOrigin - vEyePos local vForward = aEyesRot:Forward() local nViewX = math.tan(nFOV * math.pi / 360) if (nViewX == 0) then vForward:Mul(vForward:Dot(vOffset)) vEyePos:Add(vForward) return vEyePos end -- FIXME: LocalPlayer():GetFOV() should be replaced with EyeFOV() when it's binded local nWorldX = math.tan(LocalPlayer():GetFOV() * math.pi / 360) if (nWorldX == 0) then vForward:Mul(vForward:Dot(vOffset)) vEyePos:Add(vForward) return vEyePos end local vRight = aEyesRot:Right() local vUp = aEyesRot:Up() if (bFrom) then local nFactor = nWorldX / nViewX vRight:Mul(vRight:Dot(vOffset) * nFactor) vUp:Mul(vUp:Dot(vOffset) * nFactor) else local nFactor = nViewX / nWorldX vRight:Mul(vRight:Dot(vOffset) * nFactor) vUp:Mul(vUp:Dot(vOffset) * nFactor) end vForward:Mul(vForward:Dot(vOffset)) vEyePos:Add(vRight) vEyePos:Add(vUp) vEyePos:Add(vForward) return vEyePos end SWEP.BoxAttachment = "muzzle" SWEP.BoxMins = Vector(-2, -2, -2) SWEP.BoxMaxs = Vector(2, 2, 2) SWEP.BoxColor = Color(0, 255, 0) function SWEP:ViewModelDrawn() local pOwner = self:GetOwner() if (not pOwner:IsValid()) then return end local pViewModel = pOwner:GetViewModel() if (not pViewModel:IsValid()) then return end local uAttachment = pViewModel:LookupAttachment(self.BoxAttachment) if (uAttachment < 1) then return end local tAttachment = pViewModel:GetAttachment(uAttachment) if (tAttachment == nil) then return end -- FIXME: This should be removed when ViewModelFOV() is binded local nFOV = self.ViewModelFOV if (not isnumber(nFOV)) then nFOV = 62 end render.DrawWireframeBox(FormatViewModelAttachment(nFOV, tAttachment.Pos, false), tAttachment.Ang, self.BoxMins, self.BoxMaxs, self.BoxColor, true) end