First Person Head follow

Hey, I’ve been making a first person game lately.
In the game I would like the head to follow the camera movement.
I’ve tried searching around but found no solution sadly.
The script i’m using to make the body visible is the following:

local player = game.Players.LocalPlayer
local char = player.Character
local RunService = game:GetService("RunService")

char.Humanoid.CameraOffset = Vector3.new(0, 0, -1)

for i, v in pairs(char:GetChildren()) do
	if v:IsA("BasePart") and v.Name ~= "Head" then

		v:GetPropertyChangedSignal("LocalTransparencyModifier"):Connect(function()
			v.LocalTransparencyModifier = v.Transparency
		end)

		v.LocalTransparencyModifier = v.Transparency

	end
end

RunService.RenderStepped:Connect(function(step)
	local ray = Ray.new(char.Head.Position, ((char.Head.CFrame + char.Head.CFrame.LookVector * 2) - char.Head.Position).Position.Unit)
	local ignoreList = char:GetChildren()

	local hit, pos = game.Workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)

	if hit then
		char.Humanoid.CameraOffset = Vector3.new(0, 0, -(char.Head.Position - pos).magnitude)
	else
		char.Humanoid.CameraOffset = Vector3.new(0, 0, -1)
	end
end)

local function adjustLocalTransparency(object)
	if not object:IsA("BasePart") or object.Name == "Head" then
		return
	end    

	local function setToTransparency()
		object.LocalTransparencyModifier = object.Transparency
	end

	object:GetPropertyChangedSignal("LocalTransparencyModifier"):Connect(function()
		setToTransparency()
	end)

	setToTransparency()
end

for _, child in char:GetChildren() do
	adjustLocalTransparency(child)
end

char.ChildAdded:Connect(adjustLocalTransparency)

For an example, this is how i would like it to be:
image

Thanks again, have a nice day.

1 Like

I think you should make a separate script for the head. The code below should work

head = game.Players.LocalPlayer.Character:WaitForChild("Head")
camera = workspace.CurrentCamera
while true do
wait()
head.CFrame = camera.CFrame
end

If it doesn’t work, I’m sorry. Make sure to put it in a local script inside of StarterCharacterScripts.

This is what happens with that script

Oh, I didn’t think you attached the camera to the head. I might not have payed attention to the whole entire script. Sorry.

1 Like

It’s ok, thanks anyways for trying to help.

You want the head to follow the mouse (where the player looks)

I mean it could work but i think it’s best to mess with the weld instead of the head cframe.

There may be a better way to do this, but this may help. I put this LocalScript inside of StarterCharacterScripts. The server probably does not see the head movement. However, you can change that with the use of a RemoteEvent.

Code:

-- // VARIABLES
-- / Services
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

-- / Local Objects
local player = Players.LocalPlayer
local mouse = player:GetMouse()

-- / Character
local character = script.Parent
local rootPart = character.HumanoidRootPart

local neckJoint = character:FindFirstChild("Neck", true)
local neckY = neckJoint.C1.Y

-- // MAIN
RunService.RenderStepped:Connect(function()
    local mousePos = rootPart.CFrame:ToObjectSpace(mouse.Origin).LookVector

    local offsetCf = CFrame.new(0, neckY, 0)
    local angleCf = CFrame.Angles(-mousePos.Y, mousePos.X, 0)

    neckJoint.C1 = offsetCf * angleCf
end)

EDIT: I mixed up the mouse.X and mouse.Y. I changed it.

5 Likes

I have this

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
game["Run Service"].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)

Place this in a local script in startcharacterscript

1 Like

Thank you a lot! it works :smiley:
have a nice day

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.