What I Want to Achieve
Hi guys, I’m developing a game where the user’s character faces the mouse, like this. I’m using an AlignOrientation, and for the most part, it works fine.
local RS = game:GetService("RunService")
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local pp = char:WaitForChild("HumanoidRootPart")
local mouse = plr:GetMouse()
--The code below can be swapped with a BodyGyro with little to no difference
local A0 = Instance.new("Attachment")
A0.Name = "AlignAttachment"
A0.Parent = pp
local A1 = workspace.AttachmentPart.Attachment --AttachmentPart is just an anchored part with an attachment
local align = Instance.new("AlignOrientation")
align.MaxTorque = 100000
align.Responsiveness = 100
align.Attachment0 = A0
align.Attachment1 = A1
align.Parent = pp
RS.RenderStepped:Connect(function()
local mousePos = mouse.Hit.p
local xDif = pp.CFrame.X - mousePos.X
local zDif = mousePos.Z - pp.CFrame.Z
local theta = math.atan2(zDif, xDif)
A1.Orientation = Vector3.new(0, math.deg(theta)+90, 0)
end)
The Problem:
When a massless, can-collide-false, unanchored part is welded to or unwelded from the character, the turning gets a little weird. It looks as if there’s some mass offsetting the center of rotation, and it’s hard to ignore. It looks like this.
Some Things I’ve Noticed:
-This happens with both client-created and server-created welds.
-This issue also comes up when a tool is equipped or unequipped (presumably since tools are automatically welded when they’re equipped).
-I’ve seen zero difference when I use a BodyGyro as opposed to an AlignOrientation for character movement. Setting the CFrame directly is bad since movement looks choppy for other clients.
-If the character is moving when the weld is created or destroyed, the issue seems to be noticeably worse.
-If the weld is made right when the character is created (such as with CharacterAdded or at the beginning of a localscript in StarterCharacterScripts), there appears to be no problem at all.
-If the camera script starts out disabled but is enabled after a weld happens, the problem then still occurs.
-It seems to happen with welds to every body part–it’s less noticeable with HumanoidRootPart, though it still exists there.
-If I set the C0 of a weld to have an offset, the problem doesn’t seem to get any better or worse.
-It doesn’t seem to get worse if I add more and more welded parts.
-Disabling/enabling and deleting/re-pasting the camera script along with the instances the script creates doesn’t seem to help.
-Anchoring/unanchoring the welded part and parts of the character doesn’t seem to help either.
Here’s the code I’ve been using for debugging–it easily creates and destroys parts welded to the character:
local UIS = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
UIS.InputBegan:Connect(function(input, gp)
--Press 'E' to create a new part
if(input.KeyCode == Enum.KeyCode.E) then
--Create the part to be welded
local part = Instance.new("Part")
part.Massless = true
part.CanCollide = false
part.Name = "WeldedPart"
--Weld the part to character's HumanoidRootPart
local weld = Instance.new("Weld")
weld.Part0 = part
weld.Part1 = char.UpperTorso
weld.Parent = part
--Set the part's parent
part.Parent = char
--Press 'Q' to delete the part
elseif(input.KeyCode == Enum.KeyCode.Q) then
if(char:FindFirstChild("WeldedPart")) then
char.WeldedPart:Destroy()
end
end
end)
I would really appreciate help. Thanks, and have a good day!