How would I make the camera follow a target with an offset to have it shown at a screen point?

What I want to achieve is to have the player’s camera align with a target in a way that the target appears at a certain screen position. E.g., I want it to be shown at {0.3, 0}, {0.5, 0}.

The issue I’m having is that I can’t figure out what calculations I’d need for this. I know it would need to account for the camera’s field of view and the distance to the target, but I just don’t know how I’d go about doing this.

I’ve tried using camera:ScreenPointToRay() but I didn’t get far with it. I’ve also tried searching the Creator Hub and other sites for solutions but I couldn’t find any.

If anyone could just push me in the right direction, that would be greatly appreciated.

Here’s how I currently position my camera:

-- Distance to point (All axis)
local offsetDistance = 80

-- In a loop
local finalCFrame = CFrame.lookAt(targetPosition + Vector3.new(1, 1, -1) * offsetDistance, targetPosition)

Picture examples:

1 Like

Try ScreenViewportToWorld (there was some named like that)

It’s based on Vector3 but there is some version of it which accounts GUI, it’s real based vector3

Task.wait() can used as loop (becaused it’s 60 rate fps locked)

2 Likes

Ok, I tested that, and it’s definitely leading me in the right direction, thanks.
Here’s where I’m at now. I’ll try properly incorporating it, and I’ll let you know how it goes when I can!

After a bit of trial and error, I got a result I’m happy with for now. It’s not perfect, but it’s good enough.
Also, I figured out camera:ScreenPointToRay() does work for this, I was just using it wrong xd

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