I’m trying to get my character’s Torso and Head to face the camera. So far the math in it isn’t that hard, but I faced a problem which I have no idea how to fix.
Problem
So, when I move my character, it’s like it offsets the mouse position or something. I have no idea why this happens.
Code
local Player = game.Players.LocalPlayer
local Camera = workspace.CurrentCamera
local Mouse = Player:GetMouse()
local UIS = game:GetService("UserInputService")
local TweenSvc = game:GetService("TweenService")
local Character = Player.Character
local HRT = Character:WaitForChild("HumanoidRootPart")
local Head = Character:WaitForChild("Head")
local Torso = Character:WaitForChild("UpperTorso").Waist
local ToAnimate = Head["Neck"]
local OriginalCFrame = ToAnimate.C0
local CFrameValue = Instance.new("CFrameValue")
local Info = TweenInfo.new(.1, Enum.EasingStyle.Quint, Enum.EasingDirection.Out)
local Props = {}
local function UpdateCFrame()
ToAnimate.C0 = CFrameValue.Value
Torso.C0 = CFrameValue.Value
end
CFrameValue.Changed:Connect(function()
UpdateCFrame()
end)
UIS.InputChanged:Connect(function(Input, GameProc)
--if not GameProc then return end
if Input.UserInputType == Enum.UserInputType.MouseMovement then
if Camera.CameraSubject:IsDescendantOf(Character) or Camera.CameraSubject:IsDescendantOf(Player) then
local Distance = (Mouse.Hit.Position - Head.CFrame.Position).Magnitude
local DifferenceX = (Mouse.Hit.Y - Head.CFrame.Y)
local DifferenceY = (Mouse.Hit.X - Head.CFrame.X)
local AngleX = math.atan(DifferenceX / Distance)
local AngleY = -(math.asin(DifferenceY / Distance))
Props.Value = OriginalCFrame * CFrame.Angles(AngleX, AngleY, 0)
local Tween = TweenSvc:Create(CFrameValue, Info, Props)
Tween:Play()
end
end
end)
Thanks for reading ig