Reputation: 85
I am trying to get the object that the user is directly looking at. Before upgrading to Unity 5 I was getting the camera object from the FPS Controller that came with the Oculus SDK and using the "camera to viewport point" or something like that to see if an object is centered on the screen. Right now this is no longer possible because they integrated the VR part in Unity. I tried using raycasts but I just can't manage to get them to go in the right direction because it looks like there are two transforms at work:
For some reason I can't figure how to do it properly, and was wondering if anyone has a working piece of code for this.
Thank you!
Upvotes: 0
Views: 813
Reputation: 1962
I've been using the center of the camera as my raycast point. (Note, to use Camera.main
you need to have the camera tagged with MainCamera.) Here's how I've been doing it:
// calculate current camera view
var cameraCenter = new Vector3(Screen.width / 2, Screen.height / 2);
Ray ray = Camera.main.ScreenPointToRay(cameraCenter);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
var obj = hit.collider.gameObject;
// etc
}
Upvotes: 0
Reputation: 1482
I think the best way is to send a ray from center eye from thec center eye and in the center eye's rotation. not along the forward direction.
What the first hit object is what you want object, and this is all.
Upvotes: 1