Raycast to an area, not a point
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
Answer by meat5000 · Oct 23, 2015 at 12:15 AM
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.
Raycast do work against colliders. I think we misunderstood each other.
In theory what I am trying to do is Raycast from a point (the camera) towards all the points of the player model and then see if at least one Raycast hit him.
Thinking of it, CapsuleCast might do the trick. Is that a good practice? It would be scaled so that it has a diameter the same as the player's height and should be cast towards the middle of the player model.
Come to think of it, I don't believe it will work. I would create a capsule and then sweep it towards the player. However, if that player was behind something (but visible), the capsule would still hit that something and stop the sweep.
Answer by Statement · Oct 23, 2015 at 01:56 PM
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 (diff) 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.
Note: The example code uses renderers bounds and not a collider.
Answer by springwater · Oct 23, 2015 at 03:38 PM
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..
Your answer
Follow this Question
Related Questions
Raycast does not seem to properly detect object it hit 0 Answers
How best to detect when a charcter object cannot move? 3 Answers
how make raycast detect in object for crosshair 0 Answers
Is there a better way to check if a player is looking at a certain object? 0 Answers
How to make a "raycast" vertically in front of the player ? 0 Answers