Raycasting an object with Oculus
I am trying design a mechanic in an Oculus rift game where Players can't look at certain objects. The way I implemented it is by using Raycast from the player to an array of visible targets. It works fine without the Rift but when i enable Virtual Reality in Player Settings it doesn't work with the rift.
I have a Player GameObject with Main Camera as a child and the script attached to it.
Here's (part of) my code:
private Camera cam;
void FindVisTarget()
{
visibleTargets.Clear();
Collider[] targetVisibleRadius = Physics.OverlapSphere(transform.position, viewRadius, targetMask);
for(int i = 0; i <targetVisibleRadius.Length; i++)
{
Transform target = targetVisibleRadius[i].transform;
Vector3 dirtoTarget = (target.position - cam.transform.position).normalized;
if (Vector3.Angle(cam.transform.forward, dirtoTarget) < viewAngle / 2)
{
float dsttoTarget = Vector3.Distance(cam.transform.position, target.position);
if (!Physics.Raycast(cam.transform.position, dirtoTarget, dsttoTarget, obstaclesMask))
{
//do stuff
visibleTargets.Add(target);
}
}
}
}
I have no idea why it doesn't work. Thank you!
Do you know at which point in the code the target you expect to hit gets rejected? Does it make it to the final ray cast, or is it rejected by one of the earlier tests?
Debug drawing is a pretty good way to debug this sort of issue. You can give debug lines a duration, so that you can draw them while you're looking around in VR and they'll still be there when you take off the headset and pause the editor to have a look.
Your answer
Follow this Question
Related Questions
Raycasting else not working properly 0 Answers
Raycast Troubleshooting 1 Answer
How to make raycast range on this script? 0 Answers
Count objects in multiple raycasts 0 Answers