Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by fernandovt · Oct 05, 2013 at 01:09 AM · guitextontriggerentercursor

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); } }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fernandovt · Oct 07, 2013 at 04:20 AM 0
Share

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); } }

avatar image TrickyHandz · Oct 07, 2013 at 04:23 AM 0
Share

@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.

avatar image TrickyHandz · Oct 07, 2013 at 04:38 AM 0
Share

@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.

avatar image fernandovt · Oct 07, 2013 at 04:42 AM 0
Share

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?

avatar image fernandovt · Oct 07, 2013 at 04:43 AM 0
Share

By the way thank you for your patience, I really apreciate your help:)

Show more comments
avatar image
0

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 = "";

}

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TrickyHandz · Oct 07, 2013 at 09:25 PM 1
Share

@fernandovt, I updated the code in my answer. It should now cover everything you needed.

avatar image fernandovt · Oct 07, 2013 at 09:46 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What's wrong with my function? It keeps telling me 'loseText' is not a member of 'UnityEngine.GUIText'. Please help, please and thank you! 0 Answers

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges