Having issues with CFrame.lookAt with a camera

So I have a new camera being made for use with a viewportframe, the camera’s position is made to be set to the position of another object, which I use as the reference point of where the camera should go using
itemPreviewCamera.CFrame = CFrame.lookAt(itemPositionReference.CFrame.Position, modelPreview.PrimaryPart.CFrame.Position)
If I understand correctly this should position the camera to the itemPositionReference (the part I am using as a reference of position) and point it torwards modelPreview (the item displayed within the viewportframe)

However, after a bit of (albeit not entirely accurate) fiddling (as studio for some reason doesn’t visually display where cameras are) I managed to determine that the camera is just sitting in the sky around the middle of the workspace, not facing the target, which means neither part of the lookAt worked.

The full code if needed for some reason

player = game:GetService(“Players”).LocalPlayer
itemPreviewCamera = Instance.new(“Camera”)
itemPreviewCamera.Name = “Item Preview Camera”
itemPreviewCamera.Parent = script.Parent
script.Parent.CurrentCamera = itemPreviewCamera`

modelPreview = nil`

local model = player.Backpack:FindFirstChildWhichIsA(“Model”)
modelPreview = model:Clone()
modelPreview.Parent = script.Parent
local itemPositionReference = workspace[“Item Reference”][“Position Reference”]
itemPreviewCamera.CFrame = CFrame.lookAt(itemPositionReference.CFrame.Position, modelPreview.PrimaryPart.CFrame.Position)

modelPreview = nil

I’m not too experienced with viewportframes or cameras, and am I bit low knowledged on CFrames, so apologies.

idk how much of that code is commented out because of the word wrap, but you’re placing both the camera and testpart at itemPositionReference and making them both look at modelPreview.PrimaryPart.CFrame.Position. I’m not sure how you set the pivot in the model, but your camera can’t see the part because they’re on the same position.

and yea, you’re using CFrame.lookAt properly

a nice way to debug this is by setting up breakpoints and stepping through the code to see what all your variables/fields are set to. And if you print out a cframe, the first 3 numbers are the XYZ position, so you could use that to see if things are placed correctly
there’s info about debugging here

you should also use codeblocks like:


Oh I forgot about that.
TestPart isn’t what I’m trying to display, it’s something I was using to find the position of the camera.

Also yea forgot codeblocks exist :skull:.

are itemPositionReference:GetPivot() and itemPositionReference.CFrame.Position different? if not, then you can’t see the model because the camera is inside it

could you show what they both look like along with the camera’s cframe after this function runs when you print them?

Just realized that I meant to use cameraPositionReference instead of itemPositionReference for the lookAt(), which, after a bit of other meddling that I forgot exactly, it seems to be fixed, so uh, thanks probably.

Also didn’t help that apparently I wasn’t actually moving one of the two test models I was using properly :skull:

1 Like

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