Need help understanding the algorithm

Hello! I’m trying to make the character’s head and torso follow the camera’s LookVector. I did some researches and I found this youtube video:

I understand everything but the algorithm. Can anyone explain the algorithm to me please?

My Code:


local char = plr.CharacterAdded:Wait()

local head = char:WaitForChild("Head")

local neck = head:WaitForChild("Neck")

local neckC0 = neck.C0.p

local torso = char:WaitForChild("UpperTorso")

local waist = torso:WaitForChild("Waist")

local waistC0 = waist.C0

local cam = workspace.CurrentCamera

game:GetService("RunService").RenderStepped:Connect(function()

if neck then

local camPos = char:WaitForChild("HumanoidRootPart").CFrame:ToObjectSpace(cam.CFrame).LookVector

neck.C0 = CFrame.new(0, neck.C0.Y, 0) * CFrame.Angles(0, -camPos.X, 0) * CFrame.Angles(camPos.Y, 0, 0)

waist.C0 = CFrame.new(0, waist.C0.Y, 0) * CFrame.Angles(0, -camPos.X, 0) * CFrame.Angles(camPos.Y, 0, 0)

end

end)

Any help is apprieciated! Thanks!

1 Like

If i am right the script in the Starter Player Script take the cam direction and make the head of the player point in the same direction as the camera every time

1 Like

Thanks! You are right! But what’s its principle? I want to know how it is working, thanks! :bowing_man: :bowing_man:

1 Like

So the Line 1 get The Camera
The Line 3 go take every Player
The Line 5 Go take the Player Character
The Line 6 Take the Humanoid RootPart of the Character

The Line 8 Goes take the Neck on The HumanoidRootPart that connect the head to the body

the Line 10 just take the y of the neck joint

After that there is a function that run every time the run service has the render stepped

In that function it check if there is a neck and take the cam direction to put the head point at the same direction as the cam

2 Likes

Thanks again! But I want to know how is the algorithm calculated! (Sorry for confusing you, I’m not that good at English)

Oh sorry me to x)

So the Line That take the cam orientation Take first his CFrame and Use the Look Vector Property that give the orientation from a CFrame

And i have not much experience on the Joints because it use orientation and i don’t really understand how it work like (i have problem when animated a rig because it play it in the wrong orientation and it use joint same as you)

1 Like

RenderStepped is an event that gets fired every frame, this makes it so that you can update the head to the camera every frame to avoid it looking weird or laggy.

(Keep in mind that inappropriate use of RenderStepped can lead to significant performance issues to your game, Roblox recommends to only use it for code that works with the camera or character. We’re technically working with the camera, so don’t worry about it.)


local camPos = char:WaitForChild("HumanoidRootPart").CFrame:ToObjectSpace(cam.CFrame).LookVector

neck.C0 = CFrame.new(0, neck.C0.Y, 0) * CFrame.Angles(0, -camPos.X, 0) * CFrame.Angles(camPos.Y, 0, 0)
waist.C0 = CFrame.new(0, waist.C0.Y, 0) * CFrame.Angles(0, -camPos.X, 0) * CFrame.Angles(camPos.Y, 0, 0)

What this code does is basically get the camera’s CFrame’s LookVector (where it’s looking at). Then changes the C0 property of the weld in the neck and waist.

C0 is the offset of the CFrame, meaning we basically change the neck’s CFrame to look at the same position as to where the camera is looking at.


neck.C0 = CFrame.new(0, neck.C0.Y, 0) * CFrame.Angles(0, -camPos.X, 0) * CFrame.Angles(camPos.Y, 0, 0)
waist.C0 = CFrame.new(0, waist.C0.Y, 0) * CFrame.Angles(0, -camPos.X, 0) * CFrame.Angles(camPos.Y, 0, 0)

To be more specific: it changes the C0 property to a new CFrame that takes the neck’s Y level, and the angle of the camera.

2 Likes

Thanks for your detailed explanation! I’m still a little bit confused, sorry! :sweat_smile: What I want to ask is, why do we use neck.C0.Y, -camPos.X and camPos.Y?

Well from my guess, if you’re gonna change the head’s CFrame, you should probably make it stay at the same Y level. Otherwise, it’ll just probably fly to heaven LOL-

By the way, CFrame.Angles() sets the orientation. That’s what actually rotates the head.

1 Like

I just found the explanation!

This youtuber explained the algorithm very well! Thanks to him!