Search Unity

使用Unity相机发射射线的问题

Discussion in 'Scripting' started by gHao, Mar 30, 2022.

  1. gHao

    gHao

    Joined:
    Mar 26, 2019
    Posts:
    3
    当我把相机的属性,设置成如下图所示时,射线的碰撞到的点和我用鼠标点击的点有偏差
    1.png

    如果设置成 如下图所示时,就没有偏差了。
    2.png

    我想知道,如果相机的FOV角度小,会导致发射出的射线的碰撞的点和鼠标的点击的点有偏差吗?为什么会这样?还是说我的代码有问题?
    Code (CSharp):
    1.    
    2.  
    3.     public RaycastHit[] ScreenPointToRay(UdpDataReult udpDataReult)
    4.     {
    5.         ray = m_Camera.ScreenPointToRay(udpDataReult.src_point);
    6.         hitCount = Physics.RaycastNonAlloc(ray, hit, Mathf.Infinity, layerMask);
    7.  
    8.         if (hitCount > 0)
    9.         {
    10.          
    11.             hitInfo = new RaycastHit[3];
    12.             for (int i = 0; i < hit.Length; i++)
    13.             {
    14.                 if (hit[i].transform == null)
    15.                     continue;
    16.                 if (hit[i].transform.gameObject.layer.Equals(llayerMask_Target_Int))
    17.                 {
    18.                     if (hitInfo[0].collider == null)
    19.                         hitInfo[0] = hit[i];
    20.                     if (hitInfo[0].distance > hit[i].distance)
    21.                         hitInfo[0] = hit[i];
    22.                 }
    23.                 else if (hit[i].transform.gameObject.layer.Equals(layerMask_Default_Int))
    24.                 {
    25.                     if (hitInfo[1].collider == null)
    26.                         hitInfo[1] = hit[i];
    27.                     if (hitInfo[1].distance > hit[i].distance)
    28.                         hitInfo[1] = hit[i];
    29.                 }
    30.                 else if (hit[i].transform.gameObject.layer.Equals(layerMaskSuimono_Water_Int))
    31.                 {
    32.                     if (hitInfo[2].collider == null)
    33.                         hitInfo[2] = hit[i];
    34.                     if (hitInfo[2].distance > hit[i].distance)
    35.                         hitInfo[2] = hit[i];
    36.                 }
    37.                 if (hitInfo[0].collider != null && hitInfo[1].collider != null)
    38.                     break;
    39.             }
    40.         }
    41.         else
    42.             hitInfo = new RaycastHit[2];
    43.         return hitInfo;
    44.     }
     
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    "When I set the properties of the camera as shown in the figure below, there is a deviation between the point where the ray hits and the point I clicked with the mouse."

    "If it is set as shown in the figure below, there is no deviation."

    "I want to know, if the FOV angle of the camera is small, will the collision point of the emitted ray be deviated from the point of the mouse click? Why is this happening? Or is there something wrong with my code?"

    These are the three paragraphs in the forum's native language. Can we see the mouse input or screen input coordinates?
     
    Bunny83 likes this.
  3. gHao

    gHao

    Joined:
    Mar 26, 2019
    Posts:
    3
  4. gHao

    gHao

    Joined:
    Mar 26, 2019
    Posts:
    3
    upload_2022-3-30_19-26-38.png
    :mad::mad::mad:

    upload_2022-3-30_19-29-25.png
     
    Kurt-Dekker and hippocoder like this.
  5. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Sorry for the difficulty using this forum! :)