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.
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] 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] and immediately tests overlap with a
new Rect(0, 0, 1, 1)
I [updated the class] (
) 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.*
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…