I am testing Unity for VR and using Javascript.
I want to do the following:
If the player looks at an object for more than 3 seconds make a popup menu appear.
The popup menu would have 3 options to play 3 different animations depending on which menu item he looks at for more than 3 seconds.
The idea is the basic VR headsets don’t have a ‘button’ to make a selection so I am using ‘look at’.
Any help at beginners level please.
Cheers
Steve Warby
I think the best option would be to have a crosshair that is used as the “selector”. If you use the entire camera’s frustum to detect if it’s looking at something, you could have 20 different options in your view, and no idea which one is selected. Here’s an example of that.
If you want to do that, all you’d need is a UI image that’s scaled small and centered, and use a script like this:
RaycastHit hit;
bool isCounting = false;
float countdown;
public float time = 3;
void Update() {
if(Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit)) {
if(hit.transform.CompareTag("Option")) isCounting = true;
}
else {
isCounting = false;
//This happens every frame the user isn't looking at an "option" so this could be optimized. But not a big problem.
countdown = time;
}
if(countdown <= 0) {
//Select option
hit.transform.GetComponent<Option>().DoSomething();
}
if(isCounting) countdown -=Time.deltaTime;
}
I wrote this in the browser, so it’s not tested. Let me know if there’s problems. Also keep in mind some of it is arbitrary such as GetComponent(), so you’d want to change that to what you want it to really do.