Make A Mask Always In Camera of player

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

Im trying to make the mask always stay on camera

  1. What is the issue? Include screenshots / videos if possible!

Its offset


  1. What solutions have you tried so far? Did you look for solutions on the Creator Hub?

Making The Player Head Follow The Mouse

--[[
Original: Unknown, Upgraded by: Bmw
]]--
wait()

local Ang = CFrame.Angles
local aSin = math.asin
local aTan = math.atan

local Cam = game.Workspace.CurrentCamera

local Plr = game.Players.LocalPlayer
local Mouse = Plr:GetMouse()
local Body = Plr.Character or Plr.CharacterAdded:wait()
local Head = Body:WaitForChild("Head")
local Hum = Body:WaitForChild("Humanoid")
local Core = Body:WaitForChild("HumanoidRootPart")
local IsR6 = (Hum.RigType.Value==0)
local Trso = (IsR6 and Body:WaitForChild("Torso")) or Body:WaitForChild("UpperTorso")
local Neck = (IsR6 and Trso:WaitForChild("Neck")) or Head:WaitForChild("Neck")
local Waist = (not IsR6 and Trso:WaitForChild("Waist"))


local MseGuide = false

local TurnCharacterToMouse = script.Config.FollowMouse.Value

local HeadHorFactor = script.Config.HeadHorFactor.Value
local HeadVertFactor = script.Config.HeadVertFactor.Value
local BodyHorFactor = script.Config.BodyHorFactor.Value
local BodyVertFactor = script.Config.BodyVertFactor.Value


local UpdateSpeed = script.Config.UpdateSpeed.Value
local NeckOrgnC0 = Neck.C0
local WaistOrgnC0 = (not IsR6 and Waist.C0)



Neck.MaxVelocity = 1/999


if TurnCharacterToMouse == true then
	MseGuide = true
	HeadHorFactor = 0
	BodyHorFactor = 0
end

game:GetService("RunService").RenderStepped:Connect(function()
	local CamCF = Cam.CoordinateFrame
	if ((IsR6 and Body["Torso"]) or Body["UpperTorso"])~=nil and Body["Head"]~=nil then	
		local TrsoLV = Trso.CFrame.lookVector
		local HdPos = Head.CFrame.p
		if IsR6 and Neck or Neck and Waist then	
			if Cam.CameraSubject:IsDescendantOf(Body) or Cam.CameraSubject:IsDescendantOf(Plr) then
				local Dist = nil;
				local Diff = nil;
				if not MseGuide then	
					Dist = (Head.CFrame.p-CamCF.p).magnitude
					Diff = Head.CFrame.Y-CamCF.Y
					if not IsR6 then	
						Neck.C0 = Neck.C0:lerp(NeckOrgnC0*Ang((aSin(Diff/Dist)*HeadVertFactor), -(((HdPos-CamCF.p).Unit):Cross(TrsoLV)).Y*HeadHorFactor, 0), UpdateSpeed/2)
						Waist.C0 = Waist.C0:lerp(WaistOrgnC0*Ang((aSin(Diff/Dist)*BodyVertFactor), -(((HdPos-CamCF.p).Unit):Cross(TrsoLV)).Y*BodyHorFactor, 0), UpdateSpeed/2)
					else	
						Neck.C0 = Neck.C0:lerp(NeckOrgnC0*Ang(-(aSin(Diff/Dist)*HeadVertFactor), 0, -(((HdPos-CamCF.p).Unit):Cross(TrsoLV)).Y*HeadHorFactor),UpdateSpeed/2)
					end
				else
					local Point = Mouse.Hit.p
					Dist = (Head.CFrame.p-Point).magnitude
					Diff = Head.CFrame.Y-Point.Y
					if not IsR6 then
						Neck.C0 = Neck.C0:lerp(NeckOrgnC0*Ang(-(aTan(Diff/Dist)*HeadVertFactor), (((HdPos-Point).Unit):Cross(TrsoLV)).Y*HeadHorFactor, 0), UpdateSpeed/2)
						Waist.C0 = Waist.C0:lerp(WaistOrgnC0*Ang(-(aTan(Diff/Dist)*BodyVertFactor), (((HdPos-Point).Unit):Cross(TrsoLV)).Y*BodyHorFactor, 0), UpdateSpeed/2)
					else
						Neck.C0 = Neck.C0:lerp(NeckOrgnC0*Ang((aTan(Diff/Dist)*HeadVertFactor), 0, (((HdPos-Point).Unit):Cross(TrsoLV)).Y*HeadHorFactor), UpdateSpeed/2)
					end
				end
			end
		end
	end
	if TurnCharacterToMouse == true then
		Hum.AutoRotate = false
		Core.CFrame = Core.CFrame:lerp(CFrame.new(Core.Position, Vector3.new(Mouse.Hit.p.x, Core.Position.Y, Mouse.Hit.p.z)), UpdateSpeed / 2)
	else
		Hum.AutoRotate = true
	end
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

To be used only if it include third person gameplay. Otherwise, if it’s first person only, you can use an accessory or a weld constraint to put the mask in front of the character head. However, you may have to change its transparency modifier when doing so.

local RunService = game:GetService("RunService")

local Camera = workspace.CurrentCamera
local Mask = ...

RunService.PreRender:Connect(function()
	Mask.CFrame = Camera.CFrame + Camera.CFrame.LookVector * 2
end)
1 Like

It works but it does this flying thing? Is there any way to fix this?

You can give the mask a offset to place a little more closer to the camera. There might be another way to accomplish what your doing here, with Ui, if my brain is thinking right you can set the mask Ui Zindex to -1, but I don’t know if that will work because I’ve never used Zindex on Ui much.

1 Like

I want to make a 3d model not a UI but thx

Just lower the number on the LookVector multiplication.
LookVector is simply the direction FORWARDS of a CFrame, and by default is in a unit (range from 0-1)

So, by lowering the the multiplication, you’re essentially changing how close/far the object is in studs.

Just think of it as this: CameraPosition + (CameraDirection * DistanceFromCamera)

local distanceFromCamera = 0.5 --// 0.5 studs
local runService = game:GetService("RunService")
local camera = workspace.CurrentCamera
local Mask = ...

runService.PreRender:Connect(function()
    Mask.CFrame = camera.CFrame + camera.CFrame.LookVector * distanceFromCamera
end)

Again, as mentioned this will only represent the mask on the client, and if you’d like to display it on the character, the easiest method is probably making it an accessory, but you can also attach it to the character’s head.

1 Like

Have you considered using a Viewmodel? Viewmodels are often used to create first person arms and stuff like that. (You can see how to make a Viewmodel system here) If you want the mask to show up to other players you can attach it to the player, make it invisible to the player, then just use a Viewmodel, which won’t show up to other players.

1 Like

You can reduce the number of the LookVector multiplier (2) to place it closer to the camera.

1 Like

The flying issue might be caused by a collision. Check if collision is enabled, and if so, consider turning it off. I had the same little issue when I was making a View model Gun.

1 Like

Thanks To everyone I managed to fix it with a view model!

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local camera = workspace.CurrentCamera
local runService = game:GetService(‘RunService’)

local viewModel = game.ReplicatedStorage.MaskFp:Clone()
viewModel.Parent = camera

runService.RenderStepped:Connect(function()
if plr.Character.Humanoid.Health == 0 then
if camera:FindFirstChild(‘MaskFp’) ~= nil then
camera.MaskFp:Destroy()
end

end

if camera:FindFirstChild('MaskFp') ~= nil then
	
	camera.MaskFp:SetPrimaryPartCFrame(camera.CFrame * CFrame.new(0, -.55, -0.1) * CFrame.Angles(0.5,0,0))
end

end)

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