basically, i’m trying to rotate an attachment that has a particle emitter attached to it so it looks at something, i tried using a constructed cframe but it just randomly teleported somewhere and i don’t know what to do so if someone can tell me my alternatives, or how to fix that issue i would greatly appreciate it.
First you would need to translate the attachment position to worldspace, on option would be to use someCF:PositionToWorldSpace(attachment.CFrame.Position). Using the CFrame.lookAt constructor you can get the frame the attachment would need to be in world space. Then you just translate that back into local space and set the attachments CFrame.
Code example:
local function transformAttachmentLookAt(part: Part, attachment: Attachment, target: Vector3)
local attachmentPosWorld = part.CFrame:PositionToWorldSpace(attachment.Position)
local attachmentCFWorld = CFrame.lookAt(attachmentPosWorld, target)
return part.CFrame:ToObjectSpace(attachmentCFWorld)
-- note: if you are concerned about losing position accuracy due to floating point error accumulationg you could do something like:
--[[
local attachmentCFLocal = part.CFrame:ToObjectSpace(attachmentCFWorld)
return attachmentCFLocal - attachmentCFLocal.Position + attachment.Position
]]
end
Edit: Pretty sure attachments have a world position property so you could use that instead.
OK, so my main question is how i use this; my current attempt:
local function heartbeat()
if module.firing == true then
local attachmentWorldSpace = script.Parent.Parent.Handle.firepoint.CFrame:PointToWorldSpace(script.Parent.Parent.Handle.firepoint.Position)
local attachmentCFWorld = CFrame.lookAt(attachmentWorldSpace, module.target.Position)
script.Parent.Parent.Handle.firepoint.CFrame = attachmentCFWorld
script.Parent.Parent.AlignOrientation.Enabled = true
script.Parent.Parent.AlignOrientation.CFrame = CFrame.new(module.Character.HumanoidRootPart.CFrame.Position, Vector3.new(module.target.Position.X, 0, module.target.Position.Z))
elseif module.firing == false then
script.Parent.Parent.AlignOrientation.Enabled = false
end
end
runservice.Heartbeat:Connect(heartbeat)