Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Raycast hit position?

Discussion in 'Physics' started by McPeppergames, Jul 18, 2022.

  1. McPeppergames

    McPeppergames

    Joined:
    Feb 15, 2019
    Posts:
    103
    I am testing some basic raycast hits on a plane and can not get the correct Y value?
    I am doing this:

    Code (CSharp):
    1.  
    2. Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
    3. RaycastHit hit;
    4.  
    5. if (Physics.Raycast(ray, out hit, 999,layerMask))
    So I then can get the hit.point.y value BUT even with the plane set on y=0 I always get changing negative values for hit.point.y when moving the mouse over the plane.
    What am I missing here?
     
  2. McPeppergames

    McPeppergames

    Joined:
    Feb 15, 2019
    Posts:
    103
    Here is some more detail, when printing out
    Code (CSharp):
    1. Debug.Log("Y value of hit is:" + hit.point);
    2.             Debug.Log("X,y,z value of hit is:" + hit.point.x+" "+hit.point.y+" "+hit.point.z);
    I get this output with complete messed up values for Y when printing the value of hit.point.y
     

    Attached Files:

  3. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    595
    -1.7E-15 is the same as -1.7*10^(-15) or -0.0000000000000017 so almost zero. Familiarize yourself with scientific number notation. So instead of "complete messed up values" it's only tiny bit off, 100000 times smaller than size of single atom. Small errors like this are expected with fast and approximate floating point calculations used by game engines.

    This is also why you should almost never use plain equality when comparing floating point values. And even for inequalities you often have to add some room for errors depending on whether false positives or false negatives are better in each specific situation.
     
  4. McPeppergames

    McPeppergames

    Joined:
    Feb 15, 2019
    Posts:
    103
    Thx for your help!