Hello! I’m working on a character select screen, and I have deduced how to get the head in the viewport to rotate as though it’s looking at the user’s cursor. However, due to it being a viewport, welds don’t work, and accessories don’t move with the head as a result.
I’ve tried three things: Using the “HatAttachment” on the head to position the accessory relative to the head (this resulted in somewhat random offsets, and weird rotations)
Using a predefined offset (worked decently, still had issues with consistency)
And calculating the offset before the model is cloned, then trying to maintain it
I’m sure I could figure it out, but I feel like a fresh set of eyes on the code would fix up my problem pretty quick lol
Video Example
local head = clonedModel.Head
local hatattach = head:FindFirstChild("HatAttachment")
local lookat = Vector3.new(game.Players.LocalPlayer:GetMouse().X,-game.Players.LocalPlayer:GetMouse().Y,1000)
local headcf = CFrame.new(head.Position,lookat)
local x,y,z = headcf:ToOrientation()
if x and y and z then
--Head
game:GetService('TweenService'):Create(head, TweenInfo.new(0.4, Enum.EasingStyle.Quint), {
Orientation = Vector3.new(math.deg(x), math.deg(y), math.deg(z))
}):Play()
for i,v in pairs(clonedModel:GetChildren()) do
if v:IsA('Accessory') then
game:GetService("TweenService"):Create(v.Handle,TweenInfo.new(.4,Enum.EasingStyle.Quint),{Orientation = Vector3.new(x*50,-(100-(y*125)/5),z)}):Play()
end
end
for _, accessory in pairs(clonedModel:GetChildren()) do
if accessory:IsA('Accessory') and accessory:FindFirstChild("Handle") then
local handle = accessory.Handle
-- Calculate the new position for the accessory handle based on the head's CFrame
local hatpos = head.CFrame:ToObjectSpace(handle.CFrame)
local attachmentpoint = accessory.AttachmentPoint
local newAccessoryCF = CFrame.new(head.Position + hatpos.Position,headcf.LookVector)
-- Tween both the position and orientation of the accessory handle
game:GetService('TweenService'):Create(handle, TweenInfo.new(0.4, Enum.EasingStyle.Quint), {
CFrame = head.CFrame * (hatattach.CFrame * attachmentpoint:Inverse())
}):Play()
end
end
end
Thank you!