Search Unity

Ray / Sphere cast from the Rift

Discussion in 'AR/VR (XR) Discussion' started by adam2050, Apr 26, 2015.

  1. adam2050

    adam2050

    Joined:
    Mar 8, 2015
    Posts:
    16
    Can't see to get RayCasting to work from the Rift. I have created a cube and added the following script using the OVR CenterEyeAnchor as CameraFacing GameObject. The cube appears to track perfectly to my head movments but its RayCast seems to be off by at least 15 degrees.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Gaze2 : MonoBehaviour {
    5.  
    6.     public GameObject CameraFacing;
    7.     public float Distance=10;
    8.     public float SphereSize=0.2f;
    9.     public GameObject HitName;
    10.     public GameObject TheHitName;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.         Distance = 10;
    15.         TheHitName = null;
    16.  
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.         transform.LookAt(CameraFacing.transform.position);
    22.         transform.Rotate (0.0f, 180.0f, 0.0f);
    23.         transform.position=CameraFacing.transform.position + CameraFacing.transform.rotation*Vector3.forward * .3f;
    24.  
    25.         CastSphere ();
    26.     }
    27.  
    28.  
    29.     public void CastSphere()
    30.     {
    31.         RaycastHit hit;
    32.         Vector3 p1 = transform.position;
    33.         var fwd = transform.TransformDirection (Vector3.forward);
    34.         if (Physics.SphereCast (p1, SphereSize, fwd, out hit, Distance)) {
    35.             HitName = hit.collider.gameObject;
    36.             print (HitName.name);
    37.             //return HitName;
    38.             TheHitName=HitName;
    39.         }
    40.         //return null;
    41.         TheHitName = null;
    42.  
    43.     }
    44. }
    45.  
     
  2. adam2050

    adam2050

    Joined:
    Mar 8, 2015
    Posts:
    16
    Solved. Changed from to raycast and called using
    Code (CSharp):
    1. Physics.Raycast  (CameraFacing.transform.position, CameraFacing.transform.rotation * Vector3.forward, out hit)