Search Unity

Filter out horizontal surfaces with hit.normal?

Discussion in 'Physics' started by Jelmer123, Aug 23, 2021.

  1. Jelmer123

    Jelmer123

    Joined:
    Feb 11, 2019
    Posts:
    243
    Hey
    I'm raycasting against a spatial mesh (from ARKit) and would like to filter out the floor and ceiling from the hits.

    I'm doing
    Code (CSharp):
    1.  
    2. if (Physics.Raycast(source, Camera.main.transform.forward, out hit, distance, layersForRaycast)){
    3.  
    4.     Vector3 eulerNormal = Quaternion.FromToRotation(Vector3.up, hit.normal).eulerAngles;
    5.  
    6.     if (eulerNormal.x >= 340 || eulerNormal.x <= 20 || eulerNormal.z >= 340 || eulerNormal.z <= 20)
    7.  
    8.         {
    9.  
    10.             //ignore
    11.  
    12.         }
    13.  
    14.         else
    15.  
    16.         {
    17.  
    18.             //do something
    19.  
    20.         }
    21.  
    22.     }
    23.  
    24. }
    25.  
    However, this approach seems to not work as walls also get ignored depending on the angle.
    I guess I am misunderstanding the euler values, rotations are always confusing.

    Anyone with insights?
     
  2. Jelmer123

    Jelmer123

    Joined:
    Feb 11, 2019
    Posts:
    243
    Ah, hit.normal returns a Vector3, I somehow thought it was Quaternions. This makes it much easier.

    I think I solved it with:
    if (hit.normal.y > 0.9f || hit.normal.y < -0.9f)
    //ignore
     
    april_4_short likes this.