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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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
Add comment · 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
0

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
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 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.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Check if position is inside a collider 5 Answers

how do I call a function from another script, when Raycast Hits? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges