• Attaching an Entity to a Bone
    6 replies, posted
Hey, I basically want to attach an entity (hats, etc.) to a player's bone but I cant figure out why the entity spawns with an offset if I look at another direction than the direction I aligned the entity to the bone. ([URL="http://imgur.com/a/nwnuI"]http://imgur.com/a/nwnuI[/URL]) Edit: Exept of that everytime it spawns it has a little and always different offset. Thaty pretty annoying. [CODE] function AddAccessoryHat(player, model, AngOffset, PosOffset) player:SetEyeAngles(Angle(11.912, 89.791, 0.000)) -- temporary and ineffective attempt to fix it timer.Simple(0.5,function() if IsValid(accessory) then accessory:Remove() end local bPos, bAng = player:GetBonePosition(7) accessory = ents.Create("prop_dynamic") accessory:SetParent( player ) accessory:SetModel( model ) accessory:SetPos( bPos + PosOffset) accessory:SetAngles(bAng + AngOffset) player:DeleteOnRemove( accessory ) accessory:Fire("setparentattachmentmaintainoffset", "anim_attachment_head", 0.01 ) end) end [/CODE]
[QUOTE=|Shepherd|;50997485]Hey, I basically want to attach an entity (hats, etc.) to a player's bone but I cant figure out why the entity spawns with an offset if I look at another direction than the direction I aligned the entity to the bone. ([URL="http://imgur.com/a/nwnuI"]http://imgur.com/a/nwnuI[/URL]) Edit: Exept of that everytime it spawns it has a little and always different offset. Thaty pretty annoying. [CODE] function AddAccessoryHat(player, model, AngOffset, PosOffset) player:SetEyeAngles(Angle(11.912, 89.791, 0.000)) -- temporary and ineffective attempt to fix it timer.Simple(0.5,function() if IsValid(accessory) then accessory:Remove() end local bPos, bAng = player:GetBonePosition(7) accessory = ents.Create("prop_dynamic") accessory:SetParent( player ) accessory:SetModel( model ) accessory:SetPos( bPos + PosOffset) accessory:SetAngles(bAng + AngOffset) player:DeleteOnRemove( accessory ) accessory:Fire("setparentattachmentmaintainoffset", "anim_attachment_head", 0.01 ) end) end [/CODE][/QUOTE] [CODE]PimpHat = PimpHat || ClientsideModel("models/mrgiggles/sasshats/pimphat.mdl", RENDERGROUP_OPAQUE); hook.Add("RenderScene", "Pimphat", function() local owner = nil; for k,v in next, player.GetAll() do if(v:GetNWBool("m_bIsPimp")) then owner = v; break; end end if(!owner || !owner:IsValid() || owner:IsDormant() || !owner:Alive()) then PimpHat:SetNoDraw(true); return; end local id = owner:LookupAttachment("eyes"); if(!id) then return; end PimpHat:SetNoDraw(false); local pos = owner:GetAttachment(id).Pos; local ang = owner:GetAttachment(id).Ang; if(owner:Team() == TEAM_SAINTS) then PimpHat:SetPos(pos - ang:Forward() * 4.15 + ang:Up() * 3.09); PimpHat:SetAngles(ang); elseif(owner:Team() == TEAM_ROLLERZ) then PimpHat:SetPos(pos - ang:Forward() * 3.5 + ang:Up() * 2.5); PimpHat:SetAngles(ang); end end);[/CODE] that's what i do for my gamemode
[QUOTE=meirl;50998021] that's what i do for my gamemode[/QUOTE] I tried it the way you do it but it only works if I don't align it after the entity was attached to the player. [CODE] function AddAccessoryHat(player, model, AngOffset, PosOffset) --player:SetEyeAngles(Angle(11.912, 89.791, 0.000)) -- temp fix for offset timer.Simple(0.5,function() if IsValid(accessory) then accessory:Remove() end --PrintTable(player:GetAttachments()) local obj = player:LookupAttachment("anim_attachment_head") --local bPos, bAng = player:GetBonePosition(7) local tblap = player:GetAttachment(obj) local bPos = tblap["Pos"] local bAng = tblap["Ang"] accessory = ents.Create("prop_dynamic") accessory:SetParent( player ) accessory:SetModel( model ) accessory:SetPos( bPos - bAng:Forward() + bAng:Up()) accessory:SetAngles(bAng) player:DeleteOnRemove( accessory ) accessory:Fire("setparentattachmentmaintainoffset", "anim_attachment_head", 0.01 ) accessory:SetPreventTransmit( player, false ) end) end [/CODE] That works but is not aligned correctly at all. [CODE] accessory:SetPos( bPos - bAng:Forward() + bAng:Up() + PosOffset) accessory:SetAngles(bAng + AngOffset) [/CODE] That still only works if I keep looking at the direction I looked at while I aligned it. Edit: I think it could be something with the eye angles but nothing I have tried so far worked.
I understood it now, it was a little late as I wrote my last reply. [CODE] accessory:SetPos( bPos_ - bAng_:Forward()*PosOffset[1] + bAng_:Up()*PosOffset[2] [/CODE] Can I do something similar with the angle? It would be very useful. I tried that and a few other things but nothing worked. [CODE] accessory:SetAngles(bAng_ - Angle(0,0,55)*AngOffset[3]) [/CODE]
try RotateAroundAxis [url]http://wiki.garrysmod.com/page/Angle/RotateAroundAxis[/url]
Well. It works kinda. The angle is being changed correctly but with some sort of a offset every time like the one with the position before but more random .
Alright, I fixed it by myself :D If someone should be curious how I fixed it: [CODE] bAng_:RotateAroundAxis( bAng_:Up(),AngOffset[1]) bAng_:RotateAroundAxis( bAng_:Forward(),AngOffset[2]) bAng_:RotateAroundAxis( bAng_:Right(),AngOffset[3]) [/CODE] My last question would be if I can make the entity I attach to the players invisible for the player I attached it to but only in first person and if possible not while looking at mirrors.
Sorry, you need to Log In to post a reply to this thread.