How to make character head look at a specific vector?

Hello, I’m trying to make all these rigs turn their head to face the camera so I can make a thumbnail for a game, but when their torso is rotated, they don’t look in the right place.

image

Here’s my code:

for i, char in pairs(game.Workspace.Characters:GetChildren()) do 
    local yDifference =  (char.Head.CFrame.Y - char.Torso.Neck.C0.Y)/4.5
    local direction = (Vector3.new(game.Workspace.Camera.CFrame.p.X, char.Head.Position.Y, game.Workspace.Camera.CFrame.p.Z) - char.Head.Position).Unit
    char.Torso.Neck.C0 = CFrame.new(0, yDifference, 0) * CFrame.Angles(3 * math.pi/2, 0, math.pi) * CFrame.Angles(0, 0, -math.asin(direction.X)) * CFrame.Angles(-math.asin(direction.Y), 0, 0) 
end

Why not turn their head in studio and then take a ScreenShot or make a GFX?

Because I’m adding like 7 more characters and also I will be trying multiple camera positions, it will make it much easier.

Nevermind I just figured it out.

for i, char in pairs(game.Workspace.Characters:GetChildren()) do 
    local yDifference =  (char.Head.CFrame.Y - char.Torso.Neck.C0.Y)/4.5
    local direction = (Vector3.new(game.Workspace.Camera.CFrame.p.X, char.Head.Position.Y, game.Workspace.Camera.CFrame.p.Z) - char.Head.Position).Unit
    char.Torso.Neck.C0 = CFrame.new(0, yDifference, 0) * CFrame.Angles(3 * math.pi/2, 0, math.pi) * CFrame.Angles(0, 0, -math.asin(direction.X)) * CFrame.Angles(0, 0, math.rad(-char.Torso.Orientation.Y)) * CFrame.Angles(-math.asin(direction.Y), 0, 0) 
end

just add:

* CFrame.Angles(0, 0, math.rad(-char.Torso.Orientation.Y))
1 Like