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 Le-Capitaine · Jan 22, 2014 at 07:21 PM · textmousecursor

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

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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
     }   
 }
Comment
Add comment · Show 4 · 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 Le-Capitaine · Jan 23, 2014 at 10:32 AM 0
Share

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.

avatar image fafase · Jan 23, 2014 at 10:38 AM 0
Share

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.

avatar image Ebil · Jan 23, 2014 at 10:46 AM 0
Share

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.

avatar image Le-Capitaine · Jan 23, 2014 at 01:17 PM 0
Share

It works perfectly. $$anonymous$$any thanks to all of you.

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

19 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 avatar image avatar image avatar image

Related Questions

MouseCursor.Arrow and MouseCursor.Link 3 Answers

How to stop mouse getting out of the game, my game is in webplayer? 2 Answers

Setting the mouse position to specific coordinates 5 Answers

How to start Animation left turn With the cursor 1 Answer

mouse Input Instantiate 0 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