Best way to make the character look at the mouse

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.

2 Likes

Oh boy… where did my math go wrong lmao
https://gyazo.com/961f27fbcff69b5ce1b26af3033e1169

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.

5 Likes

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.

Have you looked into previous threads regarding player character facing the mouse? A quick search and I came up with the following:

If one of the thread works for you, let me know and I’ll edit out the other threads.

1 Like

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.

https://gyazo.com/b2c05e3a69d65892efb717021934dcd0

4 Likes
2 Likes