- Home /
Object Name Displays fom Everywhere
This script is working perfectly, it display a frase on objects each time the player poses the cursor over the them(a cube with this script can become a sign that tells "Exit Door" each time we put the cursor over it. The main problem is it doesn't matter if I am 100 feet away from the object, if the cursor passes over the object, the frase will be displayed. I need some help because I can't find the way to make an extra variable that says "This will happen, only "OnTriggerEnter", so the message get's displayed when you are near this object. I'm not good at scripting, and i will really apreciate some help. ANy suggestions:)
This is the script:
var toolTipText = ""; // set this in the Inspector
private var currentToolTipText = ""; private var guiStyleFore : GUIStyle; private var guiStyleBack : GUIStyle;
function Start() { guiStyleFore = new GUIStyle(); guiStyleFore.normal.textColor = Color.white;
guiStyleFore.alignment = TextAnchor.UpperCenter ; guiStyleFore.wordWrap = true; guiStyleBack = new GUIStyle(); guiStyleBack.normal.textColor = Color.black;
guiStyleBack.alignment = TextAnchor.UpperCenter ; guiStyleBack.wordWrap = true; }
function OnMouseEnter () {
currentToolTipText = toolTipText;
}
function OnMouseExit () { currentToolTipText = ""; }
function OnGUI() { if (currentToolTipText != "") { var x = Event.current.mousePosition.x; var y = Event.current.mousePosition.y; GUI.Label (Rect (x-149,y+40,300,60), currentToolTipText, guiStyleBack); GUI.Label (Rect (x-150,y+40,300,60), currentToolTipText, guiStyleFore); } }
Answer by TrickyHandz · Oct 05, 2013 at 01:30 AM
All of the OnMouse functions in Unity raycast to an infinite distance. So it doesn't matter how far away something is when the mouse function is triggered. You might be able to limit the distance by calling for a second raycast like this:
function OnMouseOver()
{
// Create a ray from the main camera that passes
// through the mouse pointer
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit;
// do a Raycast up to 50 meters from the starting point
if (Physics.Raycast(ray, hit, 50))
currentToolTipText = toolTipText;
else
currentToolTipText = "";
}
function OnMouseExit()
{
currentToolTip = "";
}
EDIT: Code Updated to Include OnMouseExit() Call.
Everything should now work with the above snippet. When you move away from the object it will clear the text and the text will clear when the mouse is no longer over the object.
I tried it and it worked almost perfectly!!. If you point it <5mt, the name of the object appears, if you point it >5mt it doesnt appear. The only problem is that if your are walking towards it, and pointing it, it doesn't matter if you get close, the name doesnt display, you have to move away the mouse and point it again to make it work, the same if you are walking away from it, the name appears in your screen, and if you walk backwards always pointing it, the name will not get away until you stop pointing at it. Do you have any suggestions to make it work properly?, Please help i can't figure out some way to do it right:(. Here is the new script with the lines you writed:
var toolTipText = ""; // set this in the Inspector
private var currentToolTipText = ""; private var guiStyleFore : GUIStyle; private var guiStyleBack : GUIStyle;
function Start() { guiStyleFore = new GUIStyle(); guiStyleFore.normal.textColor = Color.white; guiStyleFore.alignment = TextAnchor.UpperCenter ; guiStyleFore.wordWrap = true; guiStyleBack = new GUIStyle(); guiStyleBack.normal.textColor = Color.black; guiStyleBack.alignment = TextAnchor.UpperCenter ; guiStyleBack.wordWrap = true; }
function On$$anonymous$$ouseEnter () {
// Create a ray from the main camera that passes // through the mouse pointer
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition); var hit: RaycastHit;
// do a Raycast up to 50 meters from the starting point
if (Physics.Raycast(ray, hit, 10)) currentToolTipText = toolTipText; }
function On$$anonymous$$ouseExit () { currentToolTipText = ""; }
function OnGUI() { if (currentToolTipText != "") { var x = Event.current.mousePosition.x; var y = Event.current.mousePosition.y; GUI.Label (Rect (x-149,y+40,300,60), currentToolTipText, guiStyleBack); GUI.Label (Rect (x-150,y+40,300,60), currentToolTipText, guiStyleFore); } }
@fernandovt, I'm glad it got to "almost". I updated the code. Try replacing On$$anonymous$$ouseEnter with On$$anonymous$$ouseOver, that will continously raycast while the mouse is over the object and it should behave accordingly.
@fernandovt, it is entirely possible. In fact, I use a similar method to control triggers, spawning of objects, audio and visual effects and probably a couple other things. Shoot me a message on the forums and I'll gladly help you design such a system.
Ok it worked:):), but what about the "On$$anonymous$$ouseExit", because i changed only the "On$$anonymous$$ouseEnter" to "On$$anonymous$$ouseOver" and it works in one way(When aproaching), but not in the opposite way. WHat should i write on the On$$anonymous$$ouseExit?
By the way thank you for your patience, I really apreciate your help:)
Answer by fernandovt · Oct 07, 2013 at 06:57 PM
Haha yes I realized about the problem on the OnMouseExit, your idea worked in some way, I think the last thing to make it work properly is to put the OnMouseExit somewhere on the script and it should work. By now with the following lines it works in every way except when you are going away from the object while pointing at it(in that case, the text doesn't dissapear):
function OnMouseOver() {
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit;
if (Physics.Raycast(ray, hit, 5))
currentToolTipText = toolTipText;
}
function OnMouseExit ()
{
currentToolTipText = "";
}
@fernandovt, I updated the code in my answer. It should now cover everything you needed.
It doesnt work:(, Now happens something similar as when we didn't write the On$$anonymous$$ouseExit, when we point it on the range the text appears and doesn't go away, even if I point at the object again. The only way it goes off is by walking away from it's range...
Your answer
![](https://koobas.hobune.stream/wayback/20220613115339im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
gui text button dont work 1 Answer
Editing certain words in a single NGUI Text Box 0 Answers
Fix Blurry UI text? 10 Answers
How long is a string? 3 Answers