Keeping a part facing the camera despite character orientation

What do I want to achieve?
I want a part to face away from the camera despite the characters orientation. Eg. If i look at my characters face the part should be behind the character. Just as it is in this video:

What is the issue?
When the characters orientation is any other than 0,0,0 it offsets the part so that the part isnt facing away from the camera.


This is the script

wait(1)
local player = game.Players.LocalPlayer
local part = Instance.new("Part")
part.Anchored = false
part.Massless = true
local char = player.Character or player.CharacterAdded:Wait()
part.Parent = char
local root = char.HumanoidRootPart
local cam = workspace.CurrentCamera
local weld = Instance.new("Weld",part)
weld.Part0 = root
weld.Part1 = part
while wait(0.1)do
	weld.C0 = CFrame.new(Vector3.new(0,0,0),Vector3.new(0,0,0))
	weld.C0 = CFrame.new(weld.C0.Position + cam.CFrame.LookVector.Unit * 5, weld.C0.Position + cam.CFrame.LookVector.Unit)
end

What solutions have I tried so far?
I have tried asking in discord servers and now im asking here.
I have also tinkered with this for about a day now and still cant figure it out. I think its because the HumanoidRootPart is rotating and therefor the weld.C0 is also rotating but I have tried subtracting the HumanoidRootPart lookVector from the weld.C0 but it still doesnt work.

Feel free to copy the script, paste it in a localscript inside StarterCharacterScripts and tinker around with it yourself:) Any help is appreciated!

I can tell that the CFrame setting at the end can be simplified.

while true do
	weld.C0 = CFrame.lookAt(cam.CFrame.LookVector * 5, Vector3.zero)
	task.wait(0.1)
end

If you want to remove influence from the HRP, you can use its inverse CFrame.

while true do
	weld.C0 = root.CFrame:Inverse() 
		* CFrame.lookAt(cam.CFrame.LookVector * 5, Vector3.zero)
	task.wait(0.1)
end

Let me know if this doesn’t work!

It did work to some degree but now the part isnt around the player it is around the world origin (probably because i instance them.)

That’s an easy fix, I forgot about that

while true do
	weld.C0 = root.CFrame.Rotation:Inverse() 
		* CFrame.lookAt(cam.CFrame.LookVector * 5, Vector3.zero)
	task.wait(0.1)
end
1 Like

Thank you good sir! It finally worked!. You have my graditute. Just a quick question. Where have you learnt this? By exploring CFrame yourself in some baseplate or by learning something like linear algebra? Or is there another source i’ve never heard of?

1 Like

I learned of these techniques by experimenting with systems that use CFrames, like custom camera controllers, custom character controllers, and concepts that require manipulation of space in general.

The best advice I can give is to practice. Try making a system that uses CFrames heavily (like those first two examples above) so you can feel comfortable with them. In my experience, you don’t need a high-level understanding of math, only some good spatial reasoning.

Then again, I have a high-level understanding of math… I don’t know if what I’m saying is biased, so take it with a grain of salt I guess.

1 Like