First Person Shadow

Hi there. I got this first person script. Is there a way to make the head and all it’s accessories visible? Because if I dont hide the head it’s gonna clip with the camera.

local player = game.Players.LocalPlayer
local char = player.Character
local RunService = game:GetService("RunService")
local Camera = game.Workspace.CurrentCamera
local head = char:WaitForChild("Head")

local function SetCharacterLocalTransparency(transparency)
	for i, v in pairs(char:GetChildren()) do
		if v:IsA("BasePart") and v.Name ~= "Head" then
			v.LocalTransparencyModifier = transparency
			v.CastShadow = transparency == 0
		end
	end
end

RunService.RenderStepped:Connect(function(step)
	if (Camera.Focus.p - Camera.CoordinateFrame.p).magnitude < 1 then
		SetCharacterLocalTransparency(0)
		local ray = Ray.new(head.Position, ((head.CFrame + head.CFrame.LookVector * 2) - 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, -(head.Position - pos).magnitude)
		else
			char.Humanoid.CameraOffset = Vector3.new(0, 0, -2)
		end
	else
		char.Humanoid.CameraOffset = Vector3.new(0, 0, 0)
		SetCharacterLocalTransparency(0)
	end
end)

You could try creating a separate head or separate accessories in the character and moving it a bit behind the camera

I am not sure if this still works but try setting Material of the Head to ForceField and set Transparency of this body part to -10^100. Do it all in a script.
Edit: Nevermind, it does not work. However I have an another idea. Give me some time.

I tried using your script. We can see that the head makes no shadow. However after I made the following local script in StarterCharacterScripts, the head was making a shadow:

for i, v in pairs(script.Parent:GetDescendants()) do
	if v.Name == "Handle" then 
		v.Material = Enum.Material.ForceField
		v.Transparency = -10^100
		v.Parent = workspace
	end
end
script:Destroy()
1 Like

thank you so much for your help

1 Like

Thank you for helping me. Here’s the finished code if anybody wants it too.

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local Camera = game.Workspace.CurrentCamera

local originalParents = {}

local function onDescendantAdded(descendant)
	if descendant:IsA("BasePart") then
		descendant.CollisionGroup = "Characters"
        --you need a CollisionGroup named "Characters", that doesnt collide with itself and that has all baseparts of player's character.
	end
end

local function onCharacterAdded(character)
	for _, descendant in pairs(character:GetDescendants()) do
		onDescendantAdded(descendant)
	end
	character.DescendantAdded:Connect(onDescendantAdded)
end

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(onCharacterAdded)
end)

local function raycastIgnoreCharacters(rayOrigin, rayDirection)
	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	raycastParams.IgnoreWater = true

	local ignoreList = {}
	for _, descendant in pairs(workspace:GetDescendants()) do
		if descendant:IsA("BasePart") and descendant.CollisionGroup == "Characters" then
			table.insert(ignoreList, descendant)
		end
	end
	raycastParams.FilterDescendantsInstances = ignoreList

	return workspace:Raycast(rayOrigin, rayDirection, raycastParams)
end

local function SetCharacterLocalTransparency(character, transparency)
	for _, part in pairs(character:GetChildren()) do
		if part:IsA("BasePart") and part.Name ~= "Head" then
			part.LocalTransparencyModifier = transparency
			part.CastShadow = transparency == 0
		end
	end
end

local function SetHandleProperties(character)
	for _, v in pairs(character:GetDescendants()) do
		if v.Name == "Handle" and v.Parent:IsA("Accessory") then
			originalParents[v] = v.Parent
			v.Material = Enum.Material.ForceField
			v.Transparency = -10^100
			v.Parent = workspace
		end
	end
end

local function ResetHandleProperties()
	for v, handlepar in pairs(originalParents) do
		if v and handlepar then
			v.Material = Enum.Material.Plastic
			v.Transparency = 0
			v.Parent = handlepar
		end
	end
	originalParents = {}
end

RunService.RenderStepped:Connect(function()
	local player = Players.LocalPlayer
	local character = player.Character
	if character then
		local head = character:FindFirstChild("Head")
		if head then
			if (Camera.Focus.p - Camera.CFrame.p).magnitude < 1 then
				SetCharacterLocalTransparency(character, 0)
				SetHandleProperties(character)

				local rayOrigin = head.Position
				local rayDirection = (head.CFrame.LookVector * 2)
				local raycastResult = raycastIgnoreCharacters(rayOrigin, rayDirection)

				if raycastResult then
					character.Humanoid.CameraOffset = Vector3.new(0, 0, -(head.Position - raycastResult.Position).magnitude)
				else
					character.Humanoid.CameraOffset = Vector3.new(0, 0, -2)
				end
			else
				character.Humanoid.CameraOffset = Vector3.new(0, 0, 0)
				SetCharacterLocalTransparency(character, 0)
				ResetHandleProperties()
			end
		end
	end
end)
4 Likes

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