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
1
Question by $$anonymous$$ · Dec 19, 2015 at 04:31 AM · raycastvector3trigonometry

Check if object is inside a viewing radius

Very likely theres some trigonometry to this, but i cant figure it out.

Heres an example of what im trying to accomplish. The cone is inside the viewing angle, the cube is not. And the sphere is the caster. alt text

So has anyone done this before? and if so, how did you do it?

Thanks to hexagonius, this is resolved. Solution :

 public float min = -60f;
 public float max = 60f;
 bool InsideViewAngle(Vector3 point)
 {
         Vector3 dirVec = (point - transform.position).normalized;
         float up = Vector3.Dot(transform.up, dirVec) * 90f;
         float down = Vector3.Dot(-transform.up, dirVec) * 90f;
         /*
          * if player is below the object
          * up will return -90 and down will return 90
          * 
          * if player is on the same vertical level as the object :
          * up and down will return 0
          * 
          * if player is on top :
          * up will return 90 and down will return -90
         */
 
         //if up is greater than min(-60), and is less than max(60);
         return up > minVertical && up < maxVertical;
 }

capture.png (84.2 kB)
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
2
Best Answer

Answer by hexagonius · Dec 19, 2015 at 06:13 AM

The first question is "what is considered inside the radius? center point? first sight?"

I think for the first one you can use the Dot product of vector "sphere forward" and "sphere to object", and the same with sphere backward. the result of 1 means exactly in front. a 0 means it's at a 90° to it. I think a 45° is a 0,7..something, like the sin or cos of that angle.

with the second it's easier, I guess, to programmatically create a polygoncollider2D with the shape of the view. trigger enter and exits will tell you then.

last approach would be to use cameras looking both ways. OnBecameVisible can tell when an objects comes into real view.

Comment
Add comment · Show 3 · 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 $$anonymous$$ · Dec 19, 2015 at 09:34 PM 0
Share

You didn't mention to normalize though :P

avatar image hexagonius $$anonymous$$ · Dec 19, 2015 at 09:44 PM 0
Share

yeah, well... that's what you learn when testing about that stuff, right ;)

avatar image Bunny83 · Dec 19, 2015 at 10:55 PM 0
Share

Or you simply use Vector3.Angle which does the normalizing and converting of the dot product into degrees for you ^^. So you can naturally work with angles. $$anonymous$$ultiplying the result of the dot product with "90" makes no sense. The value isn't linear as it represents the cosine of the angle between the two vectors. You have to use $$anonymous$$athf.Acos to convert it to an actual angle in radians. $$anonymous$$ultiply that result with $$anonymous$$athf.Rad2Deg and you get the angle in degrees.

And by using ILSpy you can see how Vector3.Angle is defined:

 public static float Angle(Vector3 from, Vector3 to)
 {
     return $$anonymous$$athf.Acos($$anonymous$$athf.Clamp(Vector3.Dot(from.normalized, to.normalized), -1f, 1f)) * 57.29578f;
 }

The "57.29578f" is basically what is stored in $$anonymous$$athf.Rad2Deg. It's simply (180 / PI)

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

35 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

Related Questions

raycast and trigonometry 2 Answers

How to get an offset of a RaycastHit? 0 Answers

Why multiply a vector by -2? 1 Answer

Find third point resulting in a right angle triangle with 2 known points 1 Answer

How to Spherecast/Raycast diagonally? 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