void OnDisable() { // Is there a current target? if (currentTarget != null) { currentTarget.OnGazeExit(); } // Tell pointer to exit target. if (pointer != null) { // Is there a pending Cardboard trigger? if (cardboardTrigger) { pointer.OnGazeTriggerEnd(cam); } if (currentGazeObject != null) { pointer.OnGazeExit(cam, currentGazeObject); } pointer.OnGazeDisabled(); } currentGazeObject = null; currentTarget = null; cardboardTrigger = false; }
private GameObject FindGazeTarget(float radius, out ICardboardGazeResponder responder, out Vector3 intersectPosition) { RaycastHit hit; GameObject targetObject = null; bool hitResult = false; // Use Raycast or SphereCast? if (radius > 0.0f) { // Cast a sphere against the scene. hitResult = Physics.SphereCast(transform.position, radius, transform.forward, out hit, cam.farClipPlane, mask); } else { // Cast a Ray against the scene. Ray ray = new Ray(transform.position, transform.forward); hitResult = Physics.Raycast(ray, out hit, cam.farClipPlane, mask); } // Found anything? if (hitResult) { // Set object and ICardboardGazeResponder if any. targetObject = hit.collider.gameObject; responder = targetObject.GetComponent(typeof(ICardboardGazeResponder)) as ICardboardGazeResponder; intersectPosition = transform.position + transform.forward * hit.distance; } else { // Nothing? Reset variables. intersectPosition = Vector3.zero; responder = null; } return(targetObject); }
private void HandleGaze() { // Retrieve GazePointer radius. float innerRadius = 0.0f; float outerRadius = 0.0f; if (pointer != null) { pointer.GetPointerRadius(out innerRadius, out outerRadius); } // Find what object the user is looking at. Vector3 intersectPosition; ICardboardGazeResponder target = null; GameObject targetObject = FindGazeTarget(innerRadius, out target, out intersectPosition); // Found a target? if (targetObject != null) { lastIntersectPosition = intersectPosition; // Is the object new? if (targetObject != currentGazeObject) { if (pointer != null) { pointer.OnGazeExit(cam, currentGazeObject); } if (currentTarget != null) { // Replace with current object. currentTarget.OnGazeExit(); } // Save new object. currentTarget = target; currentGazeObject = targetObject; // Inform pointer and target of gaze. if (pointer != null) { pointer.OnGazeStart(cam, currentGazeObject, intersectPosition, currentTarget != null); } if (currentTarget != null) { currentTarget.OnGazeEnter(); } } else { // Same object, inform pointer of new intersection. if (pointer != null) { pointer.OnGazeStay(cam, currentGazeObject, intersectPosition, currentTarget != null); } } } else { // Failed to find an object by inner radius. if (currentGazeObject != null) { // Already gazing an object? Check against outer radius. if (IsGazeNearObject(outerRadius, currentGazeObject, out intersectPosition)) { // Still gazing. if (pointer != null) { pointer.OnGazeStay(cam, currentGazeObject, intersectPosition, currentTarget != null); } } else { // No longer gazing any object. if (pointer != null) { pointer.OnGazeExit(cam, currentGazeObject); } if (currentTarget != null) { currentTarget.OnGazeExit(); } currentTarget = null; currentGazeObject = null; } } } }
private GameObject FindGazeTarget(float radius, out ICardboardGazeResponder responder, out Vector3 intersectPosition) { RaycastHit hit; GameObject targetObject = null; bool hitResult = false; // Use Raycast or SphereCast? if (radius > 0.0f) { // Cast a sphere against the scene. hitResult = Physics.SphereCast(transform.position, radius, transform.forward, out hit, cam.farClipPlane, mask); } else { // Cast a Ray against the scene. Ray ray = new Ray(transform.position, transform.forward); hitResult = Physics.Raycast(ray, out hit, cam.farClipPlane, mask); } // Found anything? if (hitResult) { // Set object and ICardboardGazeResponder if any. targetObject = hit.collider.gameObject; responder = targetObject.GetComponent(typeof(ICardboardGazeResponder)) as ICardboardGazeResponder; intersectPosition = transform.position + transform.forward * hit.distance; } else { // Nothing? Reset variables. intersectPosition = Vector3.zero; responder = null; } return targetObject; }