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

Question World Space Canvas, Keep UI Elements on Screen At All Times

Discussion in 'UI Toolkit' started by Razputin, Jun 16, 2023.

  1. Razputin

    Razputin

    Joined:
    Mar 31, 2013
    Posts:
    347
    Say I have an enemy, he is on screen. His attached world space UI element is directly above his head.
    Now say I have an off-screen enemy. What I want is for his attached world space UI element to be at the edge of the screen closest to him.

    Essentially if they are visible on screen, have the UI element follow them. If they are off screen, have the UI element follow them at the closest point that is still on the screen.

    I can do everything except I can't determine how to do that last bit. Any thoughts on this?

    Thanks!

    Question.png
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    This is the same concept as the quest marker and whatnot. CodeMonkey has an excellent tutorial on it, but the meat of it is simple math, you can check around at the 5 minute mark or so:

     
    Razputin likes this.
  3. Razputin

    Razputin

    Joined:
    Mar 31, 2013
    Posts:
    347
    Perfect! Thanks so much!