Hello. Posted this on the forums but I think I should have done it here, I’m looking for a simple script that on Fire1 click, will display text on screen with the description of the object that has been clicked on.
I’m very new to Unity and even more with scripting and languages, I have googled a lot and have a slight idea of what’s going on behind some scripts, but not sure how to put this one together?
Thank you for any help out there.
I’d attach a script to each object and for example’s sake, name it “Description”. Here’s a simple C# example on how you would go about using Raycast from the main camera using mousePosition, then using GetMouseButtonDown to bring up the string as a GUI Label. Also take a look at ScreenPointToRay.
using UnityEngine;
using System.Collections;
public class Description : MonoBehaviour
{
public string label = "Change this in the inspector for each object";
Ray ray;
RaycastHit hit;
bool showLabel = false;
void Update()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit, Mathf.Infinity) && Input.GetMouseButtonDown(0))
{
if(hit.collider == this.collider)
{
showLabel = true;
}
else
showLabel = false;
}
}
void OnGUI()
{
if(showLabel)
GUI.Label(new Rect(0, 0, Screen.width, 256), label);
}
}