I’m trying to make a worldspace UI window appear when a raycast hits a gameobject, that worldspace UI would then show things like item stats (name, price , etc.) and rotate towards the raycast so the player can read it correctly , then if the raycast is NOT hitting the collider anymore make it disappear ?
So far I made the UI instantiate, although it would instantiate ALOT of windows, then I just added Destroy(gameObject, 0.01f); but it seems like it’s not the right way to do it and it’s just making hundreds of windows appear and get destroyed really fast (making it SEEM like it’s one window but it really isn’t and it makes the game choppy)
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
}
}
}