I am trying to get a AI character detection working. I have a character and a CCTV camera that should detect him.
The camera has a sphere collider on itself and when this triggers, I do a raycast from the camera towards the hit object and see if it is the player. But there is a problem with this approach. The transform.position of the character is at its feet. So if the whole player is visible, except his feet, he will not get detected.
Is there a way to cast from the camera towards an area, not just a single point? That would ensure that whenever any part of the player is visible, it will get detected.
Thanks
Doesn’t sound right. Raycasts work against colliders.
Anyway, you can CapsuleCast, SphereCast or even OverlapSphere from the point of your first Raycast hit on the floor.
You could check if the game objects bounding box is within the viewspace or screenspace of that camera.
[Here’s a class I made][1] for another answer that you may use and modify to suit your needs. For example you may want to use a different camera than Camera.main and you may want to use viewspace instead of screenspace. In particular, you could make a new method based on BoundsToScreenRect
that uses [WorldToViewportPoint][2] and immediately tests overlap with a new Rect(0, 0, 1, 1)
I [updated the class][3] (
[4]) to include `OverlapsViewport` methods.
*It's possible there's another way to see if the object is within a frustum of a camera. This is just one way to do it. In such case, please do add a comment on how to approach the problem with your idea.*
[1]: https://github.com/devmelon/Answers/blob/c84139bc1c013c40fd6dcd511bb5af346910395f/Assets/Answers/newGuyInThat/SelectionUtil.cs
[2]: http://docs.unity3d.com/ScriptReference/Camera.WorldToViewportPoint.html
[3]: https://github.com/devmelon/Answers/blob/aa99ec49cdef536611d2a213349bd78cc9416e4f/Assets/Answers/newGuyInThat/SelectionUtil.cs
[4]: https://github.com/devmelon/Answers/commit/aa99ec49cdef536611d2a213349bd78cc9416e4f#diff-6230b0471c79c206f9cd05648aad86aa
There is sphere cast… it may be what you are looking for…or you could get your distance between the cam and player …instantiate a trigger collision object there and see if you characters in it…