how to calclulate offset between camera lookat y position to target y position

i have camera that look at target on x and z position of the target and on y target position with some offset(im in case that i can not know in advance what exactly the offset) , how to calclulate what the offset between the target y position to the y position where the camera forward actually look at ? (the exemple image shows in 2d but its the same idea only in 3d)

One way is to make an invisible child object on your target (raised up above it) and tell the camera to look at that child object.

Also, camera stuff is pretty tricky… you may wish to consider using Cinemachine from the Unity Package Manager.

hi thanks for reply, regarding to the first solution it unfortunately this is less suitble in my case , im bulding biliard game in 3d and i have states in the game when i can rotate my camera vertically end horizontally and i cant use static chiled object as my y look at position i need somthing dinamic because of my camera forward direction changing from time to time(dependent on the state of the game) so lets say im in state when i can rotate my camera vertically and horizontally and then i swiching to state when im not allowed to rotate the camera vertically but rather only to look at target position plus the the last y offset between the target position and where i look at y position in the last state, regardnig to cinemachine maybe but in the first place I would prefer a different solution because all the code of my camera is already built and built for specific cases and states.

Hi, how about using some trigonometry, assuming you know down looking angle of the camera, and the world distance between the camera in x,z plane and the height above the target.

float h = Mathf.Tan(alpha) * Distance(new Vector2(target.pos,x, target.pos.z), new Vector2(camera.pos.x, camera.pos,z));
float y_offset = camera.pos.y-target.pos.y - h;

1 Like