Game Menu, Player look at mouse

Okay guys this is gonna be a long one as I try to explain what I want.

So basically I have my game menu which shows the player. I want the player’s head to follow the mouse wherever it is on the screen.

Example: (Ignore the horrible drawing)

Anyways that curved line represents a rotation, and the arrow represents where I want it to look. However I want the look to be natural, so not just the head looking there (Although just the head is perfectly fine)

I honestly haven’t done anything other than screw it up multiple times because I have no idea where to start with this. An explanation with code would be much appreciated, I want to learn the code not just copy it!

Although this won’t probably achieve the effect you’re looking for, it’s a pretty good example and start, indeed, if you modify this video’s code with the Mouse position instead of the Camera direction you could probably achieve the head movement.

1 Like

Issue is that I don’t know how to use the mouses position in a 3D space.

uh this article helped me Dragging objects with the mouse
it knida explains the mouse positions in 3d

1 Like

Thanks I’ll work on it and update if it works!

1 Like

I forgot where I got this script on the forum but here it is:

local RunService = game:GetService("RunService")

local Player = game.Players.LocalPlayer
local PlayerMouse = Player:GetMouse()

local Camera = workspace.CurrentCamera

local Character = Player.Character or Player.CharacterAdded:Wait()
local Head = Character:WaitForChild("Head")
local Neck = Head:WaitForChild("Neck")

local Torso = Character:WaitForChild("UpperTorso")
local Waist = Torso:WaitForChild("Waist")

local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local NeckOriginC0 = Neck.C0
local WaistOriginC0 = Waist.C0

Neck.MaxVelocity = 1/3

RunService.RenderStepped:Connect(function() 
	local CameraCFrame = Camera.CoordinateFrame

	if Character:FindFirstChild("UpperTorso") and Character:FindFirstChild("Head") then
		local TorsoLookVector = Torso.CFrame.lookVector
		local HeadPosition = Head.CFrame.p

		if Neck and Waist then
			if Camera.CameraSubject:IsDescendantOf(Character) or Camera.CameraSubject:IsDescendantOf(Player) then
				local Point = PlayerMouse.Hit.p

				local Distance = (Head.CFrame.p - Point).magnitude
				local Difference = Head.CFrame.Y - Point.Y

				Neck.C0 = Neck.C0:lerp(NeckOriginC0 * CFrame.Angles(-(math.atan(Difference / Distance) * 0.5), (((HeadPosition - Point).Unit):Cross(TorsoLookVector)).Y * 1, 0), 0.5 / 2)
				Waist.C0 = Waist.C0:lerp(WaistOriginC0 * CFrame.Angles(-(math.atan(Difference / Distance) * 0.5), (((HeadPosition - Point).Unit):Cross(TorsoLookVector)).Y * 0.5, 0), 0.5 / 2)
			end
		end
	end	
end)