RayCasting Unity 2017.3.f10 Oculus Rift VR, not originating from center of viewport/camera
I have an issue trying to cast a ray outward directly from the center of the viewport or camera, (from the middle of the head, between the eyes) straight outward in order to hit whatever the player might be looking at.
I've tried 2 different ways, and both seem to produce the exact same result
I have tried both
Ray ray = cam.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
and
Ray ray = new Ray(m_Camera.position, m_Camera.forward);
as well as visualizing the rays with
Debug.DrawRay(ray.origin, ray.direction * m_DebugRayLength, Color.green, m_DebugRayDuration);
and
Debug.DrawRay(m_Camera.position, m_Camera.forward * m_DebugRayLength, Color.yellow, m_DebugRayDuration);
and for me the direction of the ray is good, but the origin is never the center of the camera / viewport on the Y axis. the Y axis seems to be 0, even though my camera is clearly not at 0 on the y axis. (rays shoot from the feet of my character in VR, using Oculus Rift. Unity 2017.3.f10
Here is my code in it's entirety, it's a bit of a mess, trying both options
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VRStandardAssets.Utils;
public class SiTeleport : MonoBehaviour
{
public VREyeRaycaster Raycaster;
public float Radius = 0.5f;
public Vector3 RadiusVector;
public Transform rayHitIndicatorObject;
private VRInteractiveItem oldPlatform;
private Vector3 oldHitPoint;
private CharacterController Controller;
private bool triggerIsPressed;
public Camera cam;
public Transform m_Camera;
[SerializeField] private LayerMask m_ExclusionLayers; // Layers to exclude from the raycast.
[SerializeField] private float m_RayLength = 500f; // How far into the scene the ray is cast.
[SerializeField] private bool m_ShowDebugRay; // Optionally show the debug ray.
[SerializeField] private float m_DebugRayLength = 5f; // Debug ray length.
[SerializeField] private float m_DebugRayDuration = 1f; // How long the Debug ray will remain visible.
void Awake()
{
Controller = gameObject.GetComponent<CharacterController>();
if (Controller == null)
Debug.LogWarning("OVRPlayerController: No CharacterController attached.");
}
// Use this for initialization
void Start()
{
triggerIsPressed = false;
RadiusVector = new Vector3(Radius, 0, Radius);
}
// Update is called once per frame
void Update()
{
//VRInteractiveItem platform = Raycaster.CurrentInteractible;
//if (platform != null && platform != this.oldPlatform)
//{
// Debug.Log(platform);
//}
//this.oldPlatform = platform;
EyeRaycast();
}
private void EyeRaycast()
{
Ray ray = cam.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
// Create a ray that points forwards from the camera.
//Ray ray = new Ray(m_Camera.position, m_Camera.forward);
RaycastHit hit;
// Show the debug ray if required
if (m_ShowDebugRay)
{
Debug.DrawRay(ray.origin, ray.direction * m_DebugRayLength, Color.green, m_DebugRayDuration);
//Vector3 cameraPosition = m_Camera.transform.position;
//Debug.DrawRay(m_Camera.position, m_Camera.forward * m_DebugRayLength, Color.yellow, m_DebugRayDuration);
//Debug.DrawRay(cameraPosition, m_Camera.forward * m_DebugRayLength, Color.yellow, m_DebugRayDuration);
}
// Do the raycast forweards to see if we hit an interactive item
if (Physics.Raycast(ray, out hit, m_RayLength, ~m_ExclusionLayers))
{
float triggerValue = OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger);
//Debug.Log(triggerValue);
Vector3 currentHitPoint = hit.point;
//if (currentHitPoint != oldHitPoint)
//{
// //Debug.Log(hit.point);
//}
rayHitIndicatorObject.position = currentHitPoint;
if (triggerValue > 0.5f)
{
if (triggerIsPressed)
return;
triggerIsPressed = true;
// move to location looked at
transform.position = new Vector3(currentHitPoint.x, transform.position.y, currentHitPoint.z);
}
else
{
triggerIsPressed = false;
}
}
}
}
Your answer
Follow this Question
Related Questions
GvrReticlePointer instead RAYCASTHIT.point 0 Answers
Using Zed Camera Mesh Maker VR 0 Answers
raycast activation by trigger 0 Answers