- Home /
Weird issue when trying to find objects inside field of view using Vector3.angle,Issue with checking if object is within field of view using Vector3.angle
I am trying to create a game in Unity 3d where NPC's move and look around and check which objects are inside their view angle. To calculate what objects are inside it's field of view angle i use unity's build in function Vector3.Angle:
// Get current position of the Robot.
var robotObjectPosition = currentTransform.parent.position;
// Get the direction to the object
var directionToObject = (objectToFind.transform.position - robotObjectPosition) / 2;
// Calculate the angle to the object and check if it's inside our viewAngle.
var isInsideAngle = Vector3.Angle(currentTransform.forward, directionToObject) < viewAngle / 2;
Now i have a weird problem where if i move to close to an object (a plane in the shape of a goal line in this case) the above check returns false, even though the object is still in front of it and the goal line is still clearly inside of his vision. I made a screenshot to visualize my problem. In the first image the goal line is inside of his vision, but in the second ( i moved him a small step forward) the above function returns false, even though i can clearly see he is still in front of the object. I am geussing it has something to do with the width of the other object, (because i divide the viewangle by 2) but i havent been able to find a fix for it. Help is much appreciated!
Answer by Captain_Pineapple · May 09, 2019 at 09:43 AM
Hey there,
what you try to solve here is not as simple as it sounds if you want to catch all possible cases. Your current problem is the following:
Since you take the angle between your view direction and the vector between your player object and the "wall" this will work in most situations. When you get close to a reaaaally long wall the transform position of that wall will be in its geometric center. So when you are for example somewhere close to an end of said wall, the center will be far to one of your sides. Then ofc you will not recognize the wall to be "in your field of view".
I'll sketch some options to improve what you currently do: Make sure that your environment objects don't get too big relative to your robot. This will solve most of your issues, but be careful since you will in return do a lot lot lot more calculations in return. You could also check not only for the transform position of an object but also for the edges. If any point of the box collider results in a positive check you have an object in your field of view.
You could also just rely on the edges. If an object is in front of you (project the object.position - player.position on the player.forward vector. if the object has a positive z value in your players coordinate system it is in front of you) you can calculate the angle for all edges, if the opposing edges have opposing signs the object is most probably in your field of view.
You could also switch to use multiple raycasts. Depending on the objects you want to check this might be the best solution you can apply even though objects far away might be missed. Even 10 Raycasts will provide better performance if you have too many objects to check for the field of view. You can also do multiple spherecasts with growing radius and distance to simulate a "cone shaped raycast". This is kinda stupid to implement though and will not provide best performance. It will not miss any objects though.
Hope this helps you get forward. If something was unclear feel free to ask.
Thanks! this helps! now i better know what is going on. I am going to try out some of them and post the solution i went with.
Your answer
Follow this Question
Related Questions
Detecting when an object enters a collider from the top? 1 Answer
Angle Of Ray 2 Answers
AI detection script problem 0 Answers
Angles from Directions 2 Answers
Determining if something is on the Left or Right of an Object 1 Answer