Attaching an Entity to a Bone

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. (http://imgur.com/a/nwnuI)

Edit: Exept of that everytime it spawns it has a little and always different offset. Thaty pretty annoying.



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




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);

that’s what i do for my gamemode

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.



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


That works but is not aligned correctly at all.



			accessory:SetPos( bPos - bAng:Forward() + bAng:Up() + PosOffset)
			accessory:SetAngles(bAng + AngOffset)


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.



                accessory:SetPos( bPos_ - bAng_:Forward()*PosOffset[1] + bAng_:Up()*PosOffset[2]


Can I do something similar with the angle? It would be very useful.

I tried that and a few other things but nothing worked.



		accessory:SetAngles(bAng_ - Angle(0,0,55)*AngOffset[3])


try RotateAroundAxis

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 :smiley:

If someone should be curious how I fixed it:



	                bAng_:RotateAroundAxis( bAng_:Up(),AngOffset[1])
			bAng_:RotateAroundAxis( bAng_:Forward(),AngOffset[2])
			bAng_:RotateAroundAxis( bAng_:Right(),AngOffset[3])


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.