I’m looking to make the characters torso look towards the mouse on the Y axis. Although simple, I want to do this in a smooth and efficient way. I was going to go about it by just changing to CFrame of the HumanoidRootPart, but that felt sketchy, I feel like changing the C0 of the Motor6D in upper torso is the way to go?
Will it replicate if I change motor6d? Is this going to hurt my games performance? And if changing Motor6D is the way to go, how do I go about safely editing this without breaking anything already in place.
local c = script.Parent:WaitForChild("UpperTorso"):WaitForChild("Waist")
local runService = game:GetService("RunService")
runService.RenderStepped:Connect(function(dt)
local yAngle = workspace.CurrentCamera.CFrame.LookVector.Y*math.pi
c.C0 = c.C0 * CFrame.Angles((yAngle), 0,0)
end)
So the workspace.CurrentCamera.CFrame.LookVector.Y*math.pi gives me a value from -180, to 180… which is what I would want the torsos rotation to be, but trying to set the angles with the motor6d functions completely randomly, its not a radian/degree error so I’m not sure whats going on.
Changing the Motor6D that connects the Torso or UpperTorso to the HumanoidRootPart only really makes sense if the torso moves relative to the character position. E.g. in a walking animation where the torso bops up and down.
What’s the issue with just setting the CFrame of the HumanoidRootPart directly?
As for calculating the angle that the character should have on the Y axis, your approach isn’t right. What I usually do is first projecting (the vector from the character to the mouse position in the world) onto (the horizontal plane), because that simplifies the problem to 2D rather than 3D. Once you have that 2D position vector of the mouse (relative to the character), you can get the angle of that vector relative to the world with trig. There’s a built in function for that called math.atan2. Here’s a sample script:
function getAngleBetween( p1, p2 )
--vector from p2 to p1
local d = (p2 - p1)
--flattened to 2D (d.X becomes d.X * 1, d.Y becomes d.Y * 0, etc.)
d = d * Vector3.new(1, 0, 1)
--(since I'm only using the X and Z coords I guess the flattening step was unncessary)
local angle = math.atan2(d.Z, d.X)
--the angle IN RADIANS
return angle
end
while true do
local angle = getAngleBetween(thePart.Position, theMouse.Hit.p)
thePart.CFrame = CFrame.new(thePart.Position) * CFrame.Angles(0, angle, 0)
wait()
end
I might have flipped the signs or order of the arguments to atan2, I can never remember how it’s supposed to go. You can fix it by messing with the order of them and their signs, or just multiply the angle by -1 if it’s the wrong “direction” and add a constant angle like math.pi/2 if it’s off by a constant.
That doesn’t work because the characters CFrame is being changed when they move so it just makes it extremely choppy/glitchy. Also the equation I posted does correctly get the angle which I need, just doesn’t edit it. I’m looking to purely change the Y rotation, nothing else.
Sadly none of these do what I’m intending to do, most of these are broken and don’t function correctly. I should also clarify, I want it to move relative to the Camera not mouse see the gif below for what I’m trying to do.