Need help with camera facing an object

Hello! I am making an NPC dialog system, except the only part I am stuck on is positioning the camera to face the NPC. I need the system to face the NPC no matter how the NPC is rotated.

The issue is that everything I have tried seems to not point in the right direction.

I have tried .lookAt, fromEulerAnglesXYZ, and pretty much most of the CFrame solutions you would expect.

I may have been using the right solution in the wrong way, but if anyone knows how to do this, please tell me! You can always ask questions if I did not provide enough information.

1 Like

You need to elaborate more. How exactly do you want the camera to be oriented? Do you want it to just look at the NPC, or look straight into the NPC’s face with the camera facing parallel to it?

I would like it to look straight at the NPC parallel to it.


This is an example, where the angle was right. (Sorry for the low quality, I screenshotted it off of a clip, but I had my clip software’s settings very low for Roblox Studio.)

1 Like

I’m not sure if you’re talking about looking in to the NPCs face or just forcing the camera to “focus” on the NPC.

Just saw the post above.

--[[ VARIABLES ]]--
local Camera = workspace.CurrentCamera
local NPC = workspace:WaitForChild("Dummy") --NPC Model here
local CameraPoint = Camera.CFrame.Position -- This is where the camera will sit "staring" at the NPC.
local LookAt = NPC:GetPivot().Position -- The position the camera be looking at.

-- Set Camera CFrame (CameraType must be set to "Scriptable" or alternatively bind a RenderStep function. If these are not done, the Camera's position will be set the next frame)
Camera.CFrame = CFrame.lookAt(CameraPoint, LookAt)

The code above should make the Camera face in the position of the “LookAt” variable. Just make sure the Camera type is set to Scriptable or use BindToRenderStep

Hope this helps.

Very sorry, I misunderstood you before. I would like it do to what it does in the picture always.

Using the LookVector of the NPC’s head, we offset the camera right in front of it and then use CFrame.lookAt to reorient it to look back at the head.

local distance: number = 5 --5 studs in front of the head
local pos: Vector3 = Head.Position + Head.CFrame.LookVector*distance
local cf: CFrame = CFrame.lookAt(pos, Head.Position)
camera.CFrame = cf
2 Likes

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