I have a camera which rotates around a pivot (green dot) that is always a constant X units away from that pivot. I want to find the pitch to rotate around that green dot so that the target (red square) is always 25% away from the top of the screen.
This is used for a lock on camera. I've solved for yaw and everything but I don't really know what math would be used to achieve this. Is it based on field of view? Does perspective have something to do with this? Any help would be appreciated.
edit: Was asked to provide the code so here it is:
local DISTANCE = 8 local function angleBetweenPointsXZ(a, b) local deltaZ = b.X - a.X local deltaX = b.Z - a.Z return math.atan2(deltaZ, deltaX) end local function onUpdate() local yaw = angleBetweenPointsXZ(pivot.Position, target.Position) + math.pi local pitch = 0 camera.CFrame = CFrame.new(pivot.Position) * CFrame.Angles(0, yaw, 0) * CFrame.Angles(pitch, 0, 0) * CFrame.new(0, 0, DISTANCE) end
I was hesitant to include code because this is being done in Roblox. If you aren't familiar, a CFrame is just a vector for position and a rotation matrix. So that last piece where we set the camera's CFrame, it starts at the pivot's position with no rotation, applies a rotation around global Y for yaw, applies a rotation around resulting local X for pitch and than moves a DISTANCE away from the pivot in the negative lookVector direction. In this code I am trying to solve for the pitch variable.