• 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 /
  • Help Room /
avatar image
Question by lib87 · Sep 29, 2015 at 06:52 PM · raycastraycastingangledetectionhit

Raycast hit

Hi everyone.

Simply I wanted to know if its possible to detect ( and chose) raycast hit to the object 90 degree..

In my project my raycast can hit with different angles i will share a picture and you will understand better..alt text

As you can see the raycast goes with 90 degrees from red cube BUT when it hits to the other object its not hitting with 90. I have a kind of a bool value and it detect hit. What i want is that bool value to change if only raycast hit to the other object with 90 degree..

Is there any way to do this ?

raycast.jpg (197.2 kB)
Comment

People who like this

0 Show 0
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

2 Replies

  • Sort: 
avatar image

Answer by Epicwolf · Sep 29, 2015 at 08:14 PM

You can use the normal of the object that is hit to check for 90 degree angles. So maybe something like this will work:

 if (Physics.Raycast(...)){
     // raycast hit something, check for 90 degree angles
     // assume hit is your RaycastHit variable
     // assume rayDirection is the direction that your raycast travels
     // Vector3.Angle() returns the acute angle between two vectors
     float angle = Vector3.Angle(hit.normal, rayDirection);
     if (angle % 90 == 0){
         // angle is a 90 degree angle...?
         // ... do stuff here ...
     }
 }

You might need to check the angle from hit.normal to Vector3.up as well. (Perhaps check if the angle to Vector3.up is 90?)

Comment

People who like this

0 Show 0 · 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

Answer by lib87 · Sep 30, 2015 at 06:41 PM

I used this method exactly same and im not sure if its working or not.. I did this

  float angle = Vector3.Angle(hit.normal, Vector3.up);
 
             if (hit.transform.tag == "Select" )
             {
                 print(this.transform.name + "Vuran");
                 print(hit.transform.name + "Vurulan");
                 print(angle.ToString());
 
                 if(angle % 90 != 0)
                 {
                     print("Test 1 2 Test 1); 


Sometimes this looks it works.. But sometimes i move my cube to different angle while other cubes raycast still hit and my test log hits few times.. which is weird...

Comment

People who like this

0 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 Epicwolf · Oct 01, 2015 at 03:34 PM 0
Share

Actually, you didn't do the exact same thing, (`angle % 90 != 0` should be angle % 90 == 0, and you are comparing the angle to Vector3.up) but that doesn't matter because my code didn't really work anyway :P

Here is a better way to do this:

 if (Physics.Raycast(...)){
     // assume the "direction" variable is the direction you are shooting the raycast
     float angle = Vector3.Angle(hit.normal, -direction);
 
     if (angle == 0){
         // raycast hit at 90 angle!
         // ... do stuff here ...
     }
 }

What this does is check if the direction from the surface you hit is the opposite of the direction you hit it from. (i.e. if the surface was hit straight at a 90 angle)

If this isn't working, try normalizing direction and maybe hit.normal.

Note that there must be an ABSOLUTE 90 angle in order for angle to equal 0. even 1 degree of rotation will make the angle greater than 0.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Raycast detect in object (problem) 0 Answers

raycast activation by trigger 0 Answers

Raycast does not seem to properly detect object it hit 0 Answers

An probably very easy question about Raycast system... 1 Answer

Plane.RayCast not hitting at all 0 Answers


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