Rotating an attachment to look at a position

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.

thanks,
lukethebestpug

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.

3 Likes

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)

I think this would be the correct implementation:

local attachmentWorldSpace = script.Parent.Parent.Handle.CFrame:PointToWorldSpace(script.Parent.Parent.Handle.firepoint.Position)
local attachmentCFWorld = CFrame.lookAt(attachmentWorldSpace, module.target.Position) 
script.Parent.Parent.Handle.firepoint.CFrame = script.Parent.Parent.Handle:ToObjectSpace(attachmentCFWorld)
1 Like
		script.Parent.Parent.Handle.firepoint.CFrame = script.Parent.Parent.Handle:ToObjectSpace(attachmentCFWorld)

got an error here, basically it has to be some sort of value, do i do cframe or position or what?

1 Like

Oh mb.
That should be

script.Parent.Parent.Handle.firepoint.CFrame = script.Parent.Parent.Handle.CFrame:ToObjectSpace(attachmentCFWorld)
1 Like

OK, i tried that but uhh…
RobloxScreenShot20220713_185242672
goes sideways, help?

Changed the emission direction, works like a dream.

1 Like