- Home /
Raycasting on camera corners
So, I'm trying to tackle an issue where the edge of the terrain can be seen when zoomed out to a certain point. Instead of hard-capping the range of the camera, its been suggested that I use raycasting to determine whether the void is being seen by the camera, and move the camera forward accordingly.
My question is, is there anyway to send out 4 rays from the corners of the camera instead of the center? If not, is there a work around for this issue that anyone can recommend? I've been trying to read up on raycasting, but I honestly can't seem to get a grasp on how to properly utilize it.
Thanks,
Thanks for the replies, definitely set me on the right path.
Answer by Berenger · Feb 10, 2012 at 12:46 AM
You can send rays from the corner, like that :
Ray topLeft = Camera.main.ScreenPointToRay(new Vector3(0, 0, 0));
Ray topRight = Camera.main.ScreenPointToRay(new Vector3(Screen.width, 0, 0));
Ray botRight = Camera.main.ScreenPointToRay(new Vector3(Screen.width, Screen.height, 0));
Ray botLeft = Camera.main.ScreenPointToRay(new Vector3(0, Screen.height, 0));
or
Ray topLeft = Camera.main.ViewportPointToRay (new Vector3(0, 0, 0));
Ray topRight = Camera.main.ViewportPointToRay (new Vector3(1, 0, 0));
Ray botRight = Camera.main.ViewportPointToRay (new Vector3(1, 1, 0));
Ray botLeft = Camera.main.ViewportPointToRay (new Vector3(0, 1, 0));
Thanks this definitely helped me. Not sure but seems like at least for the top answer the names might be mixed up? Copy paste from the docs "Screenspace is defined in pixels. The bottom-left of the screen is (0,0); the right-top is (pixelWidth -1,pixelHeight -1)."
Answer by MD_Reptile · Feb 10, 2012 at 01:27 AM
If you follow the tutorials on 3dbuzz they go into handling this issue with camera control, and the way the do it is to draw 4 rays FROM the camera, to the player, with a "lookAtTarget" so that if those rays get blocked, it draws the camera further in to the player, until the ray is not hit. Might want to check it out:
http://www.3dbuzz.com/vbforum/sv_showvideo.php?v=4915
Also they go into making it impossible to enter the wall partially, where you get your player up against a wall, and the like FOV allows you to see into the blank world behind it...It can stop that too, really cool tutorials there!
Answer by Owen-Reynolds · Feb 10, 2012 at 04:17 AM
If you don't even think about how to code it, but just try to draw pictures of where the camera should be as you move/turn near the edge, it gets pretty ugly. If you're running towards the edge of the map, do you suddenly get an overhead view? Running along the right edge narrows my view so I can't see left of me?
Making the terrain a little larger than you need, and raising the sides a little, looks surprisingly good. Or, plant some "tree" billboards around some edges (DnD online does this -- looks OK.) You will have to limit the camera to stay within the terrain (looking from off-terrain to where you can see the edge, that looks terrible.) But that's a simpler problem.
It's also easy to be too sensitive to something no one else would see. People know there are edges, and often expect "hi, I'm the edge of the world" signals. Take a look at professional 3D games -- lots of ways to force a bad camera view.
Your answer
![](https://koobas.hobune.stream/wayback/20220613063932im_/https://answers.unity.com/themes/thub/images/avi.jpg)