- Home /
Raycast in camera
How would one go about casting lots of rays inside of the cameras bound, plus a little extra on each sides ?
Answer by Yokimato · Oct 28, 2014 at 12:37 PM
Might need a bit more information to get you the right answer, but I can give it a shot with what you got. Reading a bit into the missing pieces I'm guessing you're trying to check if something can be seen from your camera.. below is various ways to check:
The important vector for Rays from the camera is direction. You can get access to this from the transform: camera.transform.forward
. From there, you can cast a ray in that direction from the camera's position for as far as you want.
Now, if you've got a target you're trying to see if you can hit.. you can do this more easily, by ditching the direction and using the target's position to figure out the direction vectory like so:
RaycastHit hit;
float distanceToCheck = 100f;
if (Physics.Raycast(camera.transform.position, (target.transform.position - camera.transform.position).normalized, out hit, distanceToCheck) {
// can check what hit landed on
}
Further though, if you wanted to check if your target was in the frustum of the camera you can do so like:
Plane[] planes = GeometryUtility.CalculateFrustumPlanes(camera);
if (GeometryUtility.TestPlanesAABB(planes, target.renderer.bounds)) {
// target is in the camera frustum...
}
Hope this gives you enough to get you objective done. If not, provide some further information and I can edit if necessary.
Just for some further information, I am going to make a custom occlusion culling system. Sorry for the lack of information but I was on my work break, didn't have much time. Thanks for the answer, I will try it when I get home.
Is there a way to use the frustum planes to cast rays, because I need to detect if the object can be hit by the ray, if it can then render it else don't. There is no point rendering something that is behind another object, so I don't think the testPlanesAABB will work for this.
If the target is inside the camera view frustum, then simply fire a ray at its location to deter$$anonymous$$e if it is behind something. The hit result will tell you what GameObject was hit, if it is not your target then your target is being occluded.
Precisely. First check if it's even in frustum then if so cast a ray to see if it's visible
How would I replace target.renderer.enabled with world objects on the go, because I need it to be automatically done, so I wont know what the target is.
Your answer
Follow this Question
Related Questions
Mouse pos + raycast 1 Answer
How to make a ball stay in the air longer when force is added? 1 Answer
Working with RayCast 1 Answer