Search Unity

ZPhysics.Raycast distorted when rotating

Discussion in 'Physics' started by Reahreic, Jul 20, 2017.

  1. Reahreic

    Reahreic

    Joined:
    Mar 23, 2011
    Posts:
    254
    While using a Physics.Raycast to act as a laser pointer from the Oculus Vive controller I've noticed that the more i rotate towards 90° either left or right of my forward position the more the ray it's self deflects from the controllers Vector3.forward. I've tried several fixes for this and cant seem to mitigate the problem. (Debug.Drawray seems to draw the ray where it should be and where it actually triggers when hitting a collider)

    One thing i have noted is that the angle of deviation is far greater with a short ray of 2f than a long ray of 200f.

    Image shows deviation between controller looking forward (0° on compass) and right (90° on compass).



    Code (CSharp):
    1. class test :MonoBehaviour {
    2.  
    3.     public LayerMask selectableLayer = 9;
    4.     public GameObject cursor;
    5.     public GameObject vrCamRig;            //SteamVR Camera Rig
    6.  
    7.     public float maxCursorDist = 7f;    //Max distance cursor should be from controller when not on target surface
    8.     public float cursorOffset = 0f;    //distance cursor should hover above target surface
    9.  
    10.  
    11.     private GameObject castRayFrom;        //The "right" vive controller
    12.     private LineRenderer controllerRay;    //Line renderer attached to "Right" vive controller
    13.     private RaycastHit rayHit;
    14.     private Vector3 rayFwd;
    15.    
    16.  
    17.     private void Start() {
    18.         rayHit = new RaycastHit();
    19.         castRayFrom = vrCamRig.GetComponent<SteamVR_ControllerManager>().right;
    20.         controllerRay = castRayFrom.GetComponent<LineRenderer>();
    21.     }
    22.  
    23.     private void UpdatePointer() {
    24.         rayFwd = castRayFrom.transform.forward;
    25.         //Debug.DrawRay(castRayFrom.transform.position, rayFwd, Color.green);
    26.         //@FIXME: Ray deviates from expected Vector3.Forward the closer the controllers rotation gets to -90° or 90°
    27.         if(Physics.Raycast(castRayFrom.transform.position, rayFwd, out rayHit, 100f, selectableLayer.value)) {
    28.             SetCursorPosition(rayHit.point, rayHit.normal);
    29.         } else {
    30.             SetCursorPosition(rayFwd * maxCursorDist, Vector3.back);
    31.         }
    32.     }
    33.  
    34.     private void SetCursorPosition(Vector3 pos, Vector3 posNorm) {
    35.         controllerRay.SetPosition(1, castRayFrom.transform.InverseTransformPoint(pos));
    36.  
    37.         Quaternion rotation = new Quaternion();
    38.         rotation.SetLookRotation(posNorm);
    39.         transform.localRotation = rotation;
    40.         cursor.transform.rotation = rotation;
    41.  
    42.         //@FIXME: cursor offset currently shifts along world Z axis and not cursor local Z axis
    43.         cursor.transform.localPosition = pos + (Vector3.back * cursorOffset);
    44.     }
    45. }