- Home /
Instantiating a worldspace UI on raycast hit .. am I doing it right?
Ok , so I'm very new to Unity so please go easy on me :P
I'm trying to get an effect where if you hover over an item , a worldspace UI pops up and shows you the item's stats , that window would then follow the direction of your ray and if the ray isn't hitting the item's collider anymore then it disappears...
Here's what I have so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class raycasttesthmd : MonoBehaviour {
public GameObject itemInfo;
private float time;
private GameObject oneInfoWindow;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
RaycastHit seen;
Ray raydirection = new Ray(transform.position, transform.forward);
if (Physics.Raycast(raydirection, out seen, 50f))
{
if (seen.collider.tag == "Item") { //in the editor, tag anything you want to interact with and use it here
oneInfoWindow = (GameObject) Instantiate (itemInfo, seen.point, Quaternion.identity);
Destroy (oneInfoWindow, 0.01f);
}
itemInfo.transform.Translate (transform.position);
Debug.DrawRay(transform.position, transform.forward, Color.yellow, 20); //unless you allow debug to be seen in game, this will only be viewable in the scene view
}
}
}
I think I'm doing it wrong because originally Destroy (oneInfoWindow, 0.01f); was set to 1f , and it would create ALOT of windows but they would be destroyed after 1 second. Changing it to 0.01f makes it LOOK like there's only one window, but it's really hundreds of raycast instantiated prefabs getting destroyed every 0.01, which doesn't seem very performance friendly.
(I'm doing this for a VR project so I can't use any other window in screenspace or camera space.
@yaezah I would ins$$anonymous$$d have the gameobject ready on scene, and set it as inactive. On raycast hit, i would then set it as active, and deactivate it otherwise. The code below follows your provided code, in that it calls Instantiate() once. $$anonymous$$y suggestion is to never call Instantiate() and Destroy() repeatedly, as they are super expensive.
bool panelhit = false;
bool panelInstantiated = false;
GameObject oneInfoWindow ;
void Update () {
RaycastHit seen;
Ray raydirection = new Ray(transform.position, transform.forward);
if (Physics.Raycast(raydirection, out seen, 50f))
{
if (seen.collider.tag == "Item") { //in the editor, tag anything you want to interact with and use it here
panelhit = true;
} else {
panelhit = false;
}
}
if(panelhit)
{
if (!panelInstantiated) {
oneInfoWindow = (GameObject) Instantiate (itemInfo, seen.point, Quaternion.identity);
itemInfo.transform.Translate (transform.position);
panelInstantiated = true;
}
else if (panelInstantiated) {
oneInfoWindow.SetActive = true;
}
} else if (!panelhit) {
oneInfoWindow.SetActive = false;
}
Debug.DrawRay(transform.position, transform.forward, Color.yellow, 20); //unless you allow debug to be seen in game, this will only be viewable in the scene view
}
}
Your answer