- Home /
[SOLVED] Can one produce tooltip-ish text near the cursor, using only one controller?
Here's my code:
public class cursorController : MonoBehaviour {
public GUIText cursorText;
void Start () {
cursorText.text = "";
}
void Update () {
cursorText.transform.position = Camera.main.ScreenToViewportPoint(Input.mousePosition);
}
void OnMouseEnter (Collider other) {
if (other.gameObject.tag == "pickup") cursorText.text = "A cube";
}
void OnMouseExit (Collider other) {
cursorText.text = "";
}
}
This is built off the Roll-a-ball tutorial game (which by the way is excellent), so the cubes are properly tagged "pickup".
What I want to do is for text to appear near the cursor when it's over a cube, in the style of point-and-click adventure games. Initialising cursorText.text as a non-empty string reveals that the GUI text does work properly, but doesn't switch to "A cube" when over a cube. I've tried a OnMouseEnter snippet within the cubes' controller and it seems to work, thus I'm guessing I'm using OnMouseEnter/Exit the wrong way around. Nothing like that seems to turn up in the documentation.
'Course, the obvious alternative is to repeat the OnMouseEnter/Exit snippet in every item to be tagged, but I'm almost certain there's a better way.
Answer by fafase · Jan 22, 2014 at 07:23 PM
Use raycast:
void Update() {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray,out hit 100))
{
if(hit.collider.tag == "Cube")
// make tooltip show
}
}
I don't doubt that this works, but just for the sake of knowing: is there no other way than raycasts? I'm trying to stick to things I understand so far and raycasts are a little above my level.
Raycast is no more complicated than events you are using up there. Raycast is like the blind man cane. It start from a point and goes in a direction for a certain distance(infinity if none given).
Then when this ray hits a collider, you get a report via the RaycastHit struct which is passed to the method and filled with the info.
I do not know any other way to do what you are after that would be more simple. Your event may be easier but it only works on the object it is implemented so you would have to copy paste for every object.
Also if you are not familiar with raycast - you should get familiar with it. They are extremly useful and not hard to understand, just read a lil about it you will not regret it.
It works perfectly. $$anonymous$$any thanks to all of you.