Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Showing text when the player is near an object and hovering over it

Discussion in 'Scripting' started by sue_m, Oct 24, 2020.

  1. sue_m

    sue_m

    Joined:
    Jul 17, 2020
    Posts:
    2
    I'm looking for a way to make some text appear on screen when the player is hovering the mouse over an object but only if they're also close to it. Thanks in advance.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Put a component on the object that contains data about what should be displayed on the screen (e.g. the text). Then, in Update, you can do a raycast from the camera through the mouse position (Camera.ScreenPointToRay with the mouse position) with the desired max distance. If you get a hit, get the component off the hit object and display the UI text element with the text from the component.
     
  3. sue_m

    sue_m

    Joined:
    Jul 17, 2020
    Posts:
    2
    Thanks, this was really helpful!