# Simple vector question

I’m not very good with vectors so I just need a little help. I am parenting a hat to the player and with the current code the hat is slightly in the wrong position. How would I set the position of the hat so that it is 10 units in the direction that the player is facing? Currently I am using the tf2 hat code, but I just need the hat to move slightly towards where the player is facing, any ideas? Thanks

[lua]
if owner:GetRagdollEntity() then
owner = owner:GetRagdollEntity()
elseif not owner:Alive() then return end

``````local boneindex = owner:LookupBone("ValveBiped.Bip01_Head1")
if boneindex then
local pos, ang = owner:GetBonePosition(boneindex)
if pos and pos ~= owner:GetPos() then
self.Entity:SetPos(pos + ang:Forward()*1 + ang:Right()*-1.5)
ang:RotateAroundAxis(ang:Right(),-85)
ang:RotateAroundAxis(ang:Up(),270)
self.Entity:SetAngles(ang)
self.Entity:DrawModel()
return
end
``````

[/lua]

self.Entity:SetPos(pos + ang:Forward()1+ ang:Right()-1.5+ang:Up()*10)

That seems to make it float above the player’s head :s

iirc ang:Up() should be forward/backward on the head bone

Updated

Now the mask goes to the right of the player :S

Then maybe, just maybe it’s ang:Right()?

I would just use this and then adjust the x, y, z accordingly until it sat correctly on top of his head.

I tried that, by adding Vector(0,0,10) but then when the player faces in different directions the mask changes it’s position and starts going inside the player’s head.

Sorry, ok you mean adjusting the values of the angles? I haven’t really done much with angles and vectors before, but I will try that.

Ok, well I have played around with it and whenever I change any of the angles it moves it in the other directions as well as the intended ones, then I have to adjust the other angles to compensate and then it does the same thing again. Please my project has been stopped by this and I can’t seem to get around it. Can anyone please post some working code? I would be very grateful. Thanks

What are you changing in this formula?

self.Entity:SetPos(pos + ang:Forward()*x+ ang:Right()*y+ang:Up()*z)

All you should change is the x,y, and z. Don’t touch anything else in there.
x: Moves the hat forward if you increment x, backwards if you decrement it.
y: Moves the hat right if you increment y, left if you decrement it.
x: Moves the hat up if you increment z, down if you decrement it.

All the directions (left, right, up, down, …) are in relation to the players head.

I changed just the x and then it would move in all three directions :S

That’s one fucked up head bone.

Well it could. Picture you are in a cube room and you look at one of the corners. if you want to move the hat forward on the head, you actually have to move it down, left, and right. Your forward vector isn’t always something nice like <0,0,1> . Maybe you choose the wrong bone.