- Home /
ScreenToRayPoint not working in VR, how to substitute?
I have a scene with a Sphere, Cylinder, Cube and a empty Parent prefab. I have a script that makes it (as I intend), that if I click on either of the GameObjects it puts it as a child into the Parent prefab. And when I click on either of them again, they get removed from the Parent. I need to be able to do this, as I have to move the objects I want to move at the same time, and with same direction/speed.
My script for handling the child parent is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
// Start is called before the first frame update
private GameObject parent;
void Start()
{
parent = GameObject.Find("Parent");
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hitInfo = new RaycastHit();
bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
if (hit)
{
Debug.Log("Hit " + hitInfo.transform.gameObject.name);
if (hitInfo.transform.parent != null)
{
hitInfo.transform.SetParent(null);
}
else
{
hitInfo.transform.SetParent(parent.transform);
}
}
else
{
Debug.Log("No hit");
}
}
}
}
However as this is something that should be used in VR, I have come across a big issue, that is Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo) doesn't work in VR. Is there any way to substitute that code to something else, and have it work exactly the same way?
Kind regards, Allan.
Your answer
Follow this Question
Related Questions
Raycast hits everything on the screen 2 Answers
Raycast to middle screen doesn't work 1 Answer
raycast screenpointtoray isnt working for some reason 0 Answers
Not getting right value; ScreenPointToRay 0 Answers
How to detect what I'm standing on?. 1 Answer