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…

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 ?

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?)

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…