- Home /
Choose a random point that is within view of camera
Hi guys.
I've seen many questions that ask how to check weather or not something is visible, but I am now wondering how to choose a random point within the bounds of the camera.
I have a standard rectagular plane, but the camera is angled so only a cone shape is in view.
var screenY = Random.Range(10,1120);
var screenX = Random.Range(10,630);
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(screenX,screenY,100));
Debug.DrawRay (ray.origin, ray.direction * 400, Color.yellow);
Currently my plan is to use ScreenPointToRay from the camera and then try and figure out where the ray intersects my plane, and that will give me my point. However that path seems a bit like a roundabout way of doing things, is there any other ways to do this?
I did this once, but i dont know wheather that is the best way. So:
I cast ray from all corners of game and than saved the world coordinate values i got hitting the plane. I used those values to then randomly position objects on my plane.
This works if your camera is not moving. Also on resize i guess you have to redo the process.
Answer by CodeMasterMike · Jan 17, 2013 at 07:15 AM
Little pseudo code, but maybe something like this would work?
float screenX = Random.Range(0.0f, camera.main.pixelWidth);
float screenY = Random.Range(0.0f, camera.main.pixelHeight);
float ScreenZ = Random.Range(camera.main.nearClipPlane, camera.main.farClipPlane);
Vector3 point = camera.main.ScreenToWorldPoint(new Vector3(screenX, screenY, ScreenZ));
This almost works. The problem is the Z axis here. I want the points to show up on a flat plane. farClipPlane and nearClip plane both give points that are parallel to the camera. Since my camera is angeld against my plane, the Z distance is longer at the top and shorter at the bottom. I tried manually setting the Z to what I needed right after this code, but then sometimes I get points that are off camera.
[EDIT] Damn, I missed that you angle the camera, and not the plane :-P But I leave my previous text here, in case it might give you an idea how to do it.
Hmm.. Then first, you get the x and y axis as above. Then with the z-axis, you would need to calculate little more. So if you take the boundingbox (Rect) from the plane, and with the x and y axis you should be able to figure out what the z-axis should be.
Say the boundingbox goes from (100, 100, 25) to (0, 0, 0), and the random numbers gives you (25, 25, z).
Then you can calculate where z is on the boundingbox with the help of the y-axis. Since the higher up you are on the y-axis the closer you get to 25, and the lower you are on the y-axis the closer you get to 0.
And so you should be able to get the correct z-axis, which in this case would be 6.25 (25, 25, 6.25).
The math: 25/100 = 0.25. 0.25 * 25 = 6.25
In text: RandomYaxis / $$anonymous$$axYaxis = Procentage. Procentage * $$anonymous$$axZaxis = zValue.
You should be able to get the right z-axis using a Lerp calculation. I haven't tried it, but I think it, or something similar to it, would work. It might be worth a try :-)