Edit now that Solved:
A massive thanks to Todd Wasson and DrBlort for being view patient with this, even though I went through a huge array of ideas until I actually did the first answer that was given. (Yeah, It’s the simplest one, but different to what I had actually done)
In the end, Unity now takes the input from the HMD and rotates the camera itself. aka where the headset is pointed is transform.forward.
For anyone else who was in my position, this is the code which looks for collides in world tagged “button”, and will print “this is a button” to the console
void Update () {
RaycastHit seen;
Ray raydirection = new Ray(transform.position, transform.forward);
if (Physics.Raycast(raydirection, out seen, sightlength))
{
if (seen.collider.tag == "buttons") //in the editor, tag anything you want to interact with and use it here
{
print("This is a button");
}
}
Debug.DrawRay(transform.position, transform.forward, Color.black, 1); //unless you allow debug to be seen in game, this will only be viewable in the scene view
}
}
==============================================================
Okay, so… I’m very new to coding, so please excuse any completely glaring errors.
I am attempting to attach a Raycast which follows the rotation of the Oculus Rift.
My test is to check if it is looking at a box collider on a world-space-ui and print some text.
The problem i am having is that i can’t seem to update the Raycast’s direction based on the VR Devices rotation.
Attaching code below. This code doesn’t work at the commented line, and I really need someone to tell me what i’ve done wrong
using UnityEngine;
using UnityEngine.VR;
public class CameraInteract : MonoBehaviour {
public float sightlength;
void Start () {
}
void Update () {
RaycastHit seen;
Ray raydirection = new Ray(transform.position, InputTracking.GetLocalRotation(VRnode CenterEye); // This is the problem code, and i might just been using the wrong thing entirely
if (Physics.Raycast(raydirection, out seen, sightlength))
{
if (seen.collider.tag == "buttons")
{
print("This is a button");
}
}
}
}