Hello i’m trying to create a targeting system where a GUI Box will appear when the mouse cursor is over the enemy(cube). Any tips or help or even the code itself? Thanks!
use this in enemy script
function OnMouseOver () {
GameObject.Find(“GUItextobject”).text= “locked”
}
You can either do
OnMouseOver
You will attached this script to the cube
public class HoverGUI : MonoBehaviour {
private bool active = false;
void OnGUI() {
if( !active ) {
return;
}
//Do whatever you want here
}
void OnMouseEnter() {
active = true;
}
void OnMouseExit() {
active = true;
}
}
Depending on what you are looking for, you also need to know the how to get the screen position from world position for 3D tracking purposes (Google it)
Pros
- Dead simple
Cons
- Will not work if you need different sets of UI to pop up for different objects
Raycast
Attached this to your camera or empty object
public class Raycaster: MonoBehaviour {
private Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
private RaycastHit hit;
// You can use either string or enum. Enum is a better option,
// I am too lazy to write it down here
private string guiType;
void Update() {
if (Physics.Raycast(ray, out hit, 100)) {
Process( hit.gameObject );
}
}
void Process( GameObject hitObject ) {
// Use tags or interface to determine the type of gui you want to display
// For example,
// Enemy : Display name
// Pickup : Display name
// Ammo : Display name + amount
// Button : Display instruction
}
void OnGUI() {
if( guiType == "enemy" ) {
//Display GUI
}
else if( guiType == "pickup" ) {
}
}
}
Pros
- Robust
- Can work with more objects
Cons
- Takes time to code. Takes even more time to make it work efficiently