In my game, there are 2 tanks. I define camPoint as the center of the tanks in world coordinates - and this is the point the camera points to.
Problem is that this doesn’t take into account the camera perspective. In the image, camPoint is at the yellow spot, but you can see that the tanks are not at the same distance from the edges:
The desired location for camPoint is at the pink dot, so that both tanks are at the same distance from the edge. But how do I find that exact spot? I’m sure it can be calculated, but I haven’t been able to figure it out.
Store each tank’s position on the viewport in arrays/lists.
Find the centerpoint by using the min/max as in the code below.
Convert that centerpoint back to world coordinates by using the x,y centerpoint found and for z, the typical distance to the camera.
for (var i = 0; i < numberOfTanks; i++) {
tanksX = camcam.WorldToViewportPoint(tanks*.transform.position).x;* tanksY = camcam.WorldToViewportPoint(tanks*.transform.position).y;* } tanksXmin = Mathf.Min (tanksX); tanksYmin = Mathf.Min (tanksY); tanksXmax = Mathf.Max (tanksX); tanksYmax = Mathf.Max (tanksY); camScript.camPoint = camcam.ViewportToWorldPoint(new Vector3((tanksXmax + tanksXmin) / 2, (tanksYmax + tanksYmin) / 2, camScript.camDist)); camScript.camPoint = new Vector3 (camScript.camPoint.x, tankOffsetY+camScript.zoomLevel, camScript.camPoint.z); And then in cam’s LateUpdate(): offset = new Vector3 (offsetXZ, offsetY, offsetXZ); transform.position = camPoint+offset; transform.LookAt (camPoint, Vector3.up); Notice that I’m using the Y axis as the axis Unity typically defines as ‘Z’. The strange thing is that it takes a few steps for this to actually get to the point, which is what I’m trying to fix sill.