Cam attach to head problem!

Trying to make some sort of FPS sort of feel but ran into a problem! I attached the camera to the head, but then it limits it so the character cant move its mouse left and right to rotate character!

Example:

Heres my code.

local camera = workspace.CurrentCamera
local part = character:WaitForChild("Head")
camera.CameraType = Enum.CameraType.Scriptable
camera.CameraSubject = part

local function OnChanged()
	if equipped then
		camera.CFrame = part.CFrame
	end
	wait()
end
Run.RenderStepped:Connect(OnChanged)

Any help is wonderful!

2 Likes

That’s because you’re setting the CFrame of the camera to the CFrame of the head. If you don’t want that, remove the camera.CFrame = part.CFrame.

But isnt that what locks the camera to the head?

I thought that wasn’t what you wanted? Could you please describe your problem in a little more detail?

I’m trying to lock the cam to the players head. So when The animation moves the head it moves the cam. I got that working but the code I have makes it so the character can’t move his mouse in first person. So the character can’t rotate

I think the problem is with your animation. When the animation runs then its rotates the head?

No what. The animation is what is rotating the head. It has nothing to do with the animation.

local camera = workspace.CurrentCamera
local part = character:WaitForChild("Head")
camera.CameraType = Enum.CameraType.Custom
camera.CameraSubject = part

local function OnChanged()
	if equipped then
		camera.CFrame = part.CFrame
	end
	wait()
end
Run.RenderStepped:Connect(OnChanged)

Problem was you set it scriptable (camera type) make it custom.

Also please don’t use these camera part thing.

use

  local player = game.Players.LocalPlayer
  player.CameraMode = Enum.CameraMode.LockFirstPerson

There’s this one property called CameraOffset, I forgot if it’s on camera or humanoid but that is probably what you’re looking for.

I’ve edited my code msg, please read it. It might help you.

The problem is I am locking it in first person. But the goal is do the cam follows the head to give it some sort of feel

Yes try this

To make it in third person

Link For The Post

I already have done this! I just want to make the cam follow the head. Like ive said maybe 4 times now.

Its not made by me. But you can try this.

Updated Code
----------------
--[[SETTINGS]]--
----------------

local ViewAccessories = false
local ViewHeadAccessories = false
local RealisticView = true
local AutoLockFirstPerson = true

------------
--[[CODE]]--
------------
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local Offset = Vector3.new()

local Player = Players.LocalPlayer

function Update()
	local Character = Player.Character --I'll have this here, should the script be in StarterPlayerScripts
	local Camera = workspace.CurrentCamera --In case they delete the camera or something.
	if not Character then
		return
	end
	
	local Humanoid = Character:WaitForChild("Humanoid")
	local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
	local Head = Character:WaitForChild("Head")
	
	for Index, Item in ipairs(Character:GetChildren()) do
		if Item:IsA("BasePart") then
			if Item.Name ~= "Head" then
				Item.LocalTransparencyModifier = 0
			end
		elseif Item:IsA("Accoutrement") then
			local Handle = Item:FindFirstChild("Handle")
			if Handle then
				local ConnectedToHead = (Handle:FindFirstChild("HatAttachment") or Handle:FindFirstChild("HairAttachment") or Handle:FindFirstChild("FaceAttachment") or Handle:FindFirstChild("NeckAttachment")) ~= nil
				if ConnectedToHead then
					if ViewAccessories and ViewHeadAccessories then
						Handle.LocalTransparencyModifier = 0
					end
				else
					if ViewAccessories then
						Handle.LocalTransparencyModifier = 0
					end
				end
			end
		end
	end
	
	if (RealisticView and Player.CameraMode == Enum.CameraMode.LockFirstPerson)
		or (Character:WaitForChild("Head").LocalTransparencyModifier == 1) then
		--Above: Adding [or (Character:WaitForChild("Head").LocalTransparencyModifier == 1)] will make it so we can
		--detect if the player is in first-person
		local Origin = Camera.CFrame
		local Out = Camera.CFrame * CFrame.new(0, 0, -1)
		local Y_Diff = (Origin.Y - Out.Y) * -1
		local Z_Diff = 0
		local Extra_X = 0
		local Extra_Y = 1.5
		
		local Camera_Offset = Vector3.new(0, 0, math.min(Y_Diff - Z_Diff, 0))
		
		--Update #2: R15 offsets cause issues with this code block. This next one will fix that.
		local UpperTorso = Character:FindFirstChild("HumanoidRootPart")
		if UpperTorso then
			local OffsetVal = HumanoidRootPart.CFrame:toObjectSpace(Head.CFrame).p
			Offset = Camera_Offset + (OffsetVal - Vector3.new(0, 1, 0))
		else
			Offset = Camera_Offset
		end
		
		Humanoid.CameraOffset = Offset
	else
		Humanoid.CameraOffset = Vector3.new()
	end
end

if AutoLockFirstPerson then
	Player.CameraMode = Enum.CameraMode.LockFirstPerson
end

RunService.RenderStepped:Connect(Update)

The code seems fine, although I don’t get where the Equipped variable is getting changed, as its being checked before updating the CFrame.

The equiped is the tool/gun being equiped and sets its to true/false

I’m pretty sure the only way you can achieve an aiming down sights effect is by utilising view models or making an animation.