- Home /
Isometric Camera : Get the displayed terrain portion.
How can i get the portion of the terrain i'm viewing with my isometric camera (as in the picture)? The only thing I can think of is getting the raycast of the camera to get the center of the rect (intersect with terrain) and then somehow extrapolate the four corners.
I'm using this to highlight a viewport on my minimap.
This is a pretty common problem and I think between your diagram and my code sample it came out fairly well. If you think so, would you $$anonymous$$d marking my answer as accepted so we can have this show up for future users asking similar questions?
Answer by Mortoc · Feb 06, 2014 at 03:21 PM
// assuming the ground is on it's own layer called 'ground-plane'
int mask = 1 << LayerMask.NameToLayer("ground-plane");
// get the 2 camera-corner rays
Ray topRightRay = Camera.main.ViewportPointToRay(Vector3.zero);
Ray bottomLeftRay = Camera.main.ViewportPointToRay(Vector3.one);
// find the corners of the terrain-rect
RaycastHit topRightRH;
RaycastHit bottomLeftRH;
Rect result;
if( Physics.Raycast(topRightRay, out topRightRH, Mathf.Infinity, mask) && Physics.Raycast(bottomLeftRay , out bottomLeftRH, Mathf.Infinity, mask) )
{
result = Rect.MinMaxRect(topRightRH.point.x, topRightRH.point.y, bottomLeftRH.point.x, bottomLeftRH.point.y);
}
else
{
throw new InvalidOperationException("Cannot get the terrain rect when the camera isn't completely over the terrain");
}
return result;
Was thinking of something in this direction myself, i'll check if it works when i have the time. Thanks for the reply.
From the ViewportPointToRay documentation the coordinates retrieved by Raycasting were mixed up. It should be topRight, and bottomLeft accordingly. The logic returns the desired result.
Yah, I was thinking I might have swapped that a bit - I was writing that from memory. Fixed now.