Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Having the head follow the mouse does not work?

I'm trying to get the head follow the mouse, i.e point towards the mouse - which I do by using the Head's orientation property and modifying it by the angle between the mouse's and the head's lookvectors. I thought it would be relatively simple, but apparently not.

This produces odd behaviour (video below) and I'm not sure how to fix it:

https://vimeo.com/421911748

Here is the code.

local RunService = game:GetService("RunService")

local Player = game:GetService("Players").LocalPlayer
local Head = Player.Character:WaitForChild("Head")
local Mouse = Player:GetMouse()

RunService.RenderStepped:Connect(function()
	local HeadLookVector = Head.CFrame.LookVector
	local MouseLookVector = Mouse.Hit.LookVector

	Head.Orientation = Vector3.new(math.deg(math.acos((HeadLookVector:Dot(MouseLookVector)/(HeadLookVector.Magnitude * MouseLookVector.Magnitude))), 0, 0))
end)

2 comments

0

Try just the mouses look vector. Tell me what it does though cause I’m curious lol

1 answer

So I've worked out how to do what I wanted to do, and it's totally different from what I was doing in the original post.

To those wondering, I did this (free code):

local RunService = game:GetService("RunService")

local Player = game:GetService("Players").LocalPlayer
local Neck = Player.Character:WaitForChild("Torso").Neck
local Mouse = Player:GetMouse()

local YOffSet = Neck.C0.Y

RunService.RenderStepped:Connect(function()
	local MouseLookVector = Mouse.Hit.LookVector
	
	Neck.C0 = CFrame.new(0, YOffSet, 0) * CFrame.Angles(math.rad(-90), 0, math.rad(180)) * CFrame.Angles(-math.asin(MouseLookVector.Y), 0, 0)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.