Basically, I’m trying to use the function :ToObjectSpace so the attachment can get put in the right place in the workspace, but I’m not sure how to also add the way the humanoid is looking and 7 studs in front, any help please?
CODE
local attachment = Instance.new("Attachment")
attachment.CFrame = humrp.CFrame:ToObjectSpace(humrp.CFrame.lookVector * 7)
Hmm, I tried just doing it using the lookvector of the humanoid * 7 but it still didnt work and it put the attachment no where near where it was supposed to be at
local offset = 7 -- changeable
local attachment = Instance.new("Attachment")
attachment.CFrame = CFrame.new(0, 0, -(humrp.Size.Z/2 + offset))
Attachments work differently, I usually think of it as ignoring the CFrame you first multiply with if you are multiplying from its parent which in this case you would be if it was a part. Usually if this wasn’t an attachment it would be:
I am trying to make the attachment position itself 7 studs in front of the player’s humanoid root part, onto the part, I’m using raycasting to find the part in front of the player 7 studs.
I don’t want to waste any more of your time, but could I ask why it needs to be inside the workspace? The only reason I could think of is you’re trying to use world position (solved by attachment.WorldPosition) for an endpoint while raycasting.
So if you do have a loop, you can use just use set the attachement position inside of the humanoidrootpart to the negative magnitude between the humanoidrootpart and end point
I got this to work easily, heres a video of it and my source code. I didn’t explain it the best so iI thought i’d just show you instead. https://youtu.be/aEqpSKIsxO4
local plr = game.Players.LocalPlayer
local char = plr.Character
local hrp = char.HumanoidRootPart
local attach = Instance.new("Attachment")
attach.Visible = true
attach.Parent = hrp
local maxPos = 7
local function raycast()
local ray = Ray.new(hrp.Position, hrp.CFrame.LookVector * maxPos)
return workspace:FindPartOnRay(ray, char)
end
game:GetService("RunService").RenderStepped:Connect(function()
local part,pos,surface = raycast()
local mag = (pos - hrp.Position).Magnitude
attach.Position = Vector3.new(0,0,-mag)
end)