I’m a beginner with scripting and Unity in general however I’ve made it so I can interact with an object in VR and can be thrown around and placed.
However, I now want to be able to have a text popup whenever the player is holding the object and disappear when they’re not.
Is there also a way to record interaction time? I assume it’s just a stopwatch script and assigning triggers but I’m new to that as well.
Any help would be greatly appreciated
I’d recommend looking into creating a prefab that contains a world-space canvas, then placing that prefab wherever you want in space. Another nice feature would be something on the prefab’s script to keep it facing the camera. For example, something like this in your Update statement:
this.transform.LookAt(Camera.main.transform.position);
(Caveat: I don’t know if you need to do something different for VR than Camera.main…)
You can also parent the popup to the object the player is holding, so that it moves as the object moves.
As for time recording, the simplest thing is to just keep track of a start time in some class variable, and occasionally checking whether (Time.timeSinceLevelLoad - _startTime) is greater than the duration you’re looking for. Another approach is to keep a class variable to reflect elapsed time, and perform (_elapsedTime += Time.deltaTime) every Update statement. Then you can periodically check whether _elapsedTime is long enough to do something else. Yet another approach, if you want something to happen periodically, is to use a Coroutine, and use ‘yield return new WaitForSeconds(x)’ to wait approximately a certain length of time in a while loop, performing an action at a given interval.
Thank you for your help. Will this allow the text to appear/disappear whenever the player is holding/not holding the object?
For the caveat you mentioned, wouldn’t replacing Camera.main for player work?
Yup. You can just Destroy the popup object when the player lets go of the object. So, the moment they let go, you’d destroy the popup object, and it would disappear.
I don’t know anything about VR dev. My assumption would be that there’s still a single main camera as far as the editor is concerned, and that Unity just does some magic to turn this into two camera (one for each eye) at runtime. So maybe it’s fine. Just something to be aware of in case simply changing the main camera’s transform doesn’t work.