Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by unity_MlGN0JMf_0D2MA · May 09, 2019 at 09:02 AM · aidirectionanglefield of viewvision

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!

goalline still inside view angle here

goalline no longer in viewangle according to function, even though he is still in front of it

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_MlGN0JMf_0D2MA · May 09, 2019 at 11:45 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

177 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges