Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by ZaddBuzuki · Oct 23, 2016 at 05:49 PM · camerauiguigameobjectview

How do I make UI Elements Stay on Screen, but change position, wanting to lock with gameobject position?

I have absolutely no idea if what I am trying to do even has a name at all, so please bare with me.

I want to have a UI Object on screen during the game, it is just basic text and an image behind it for contrast and it will change throughout the game, but if your character were to walk in the direction the UI is on the screen, eventually when the gameobject comes into view, you'd see the UI lock on the game object, so you know that's what is speaking etc.

Like being able to hear something from far off, but once you approach it, you can see whats actually talking. you know?

Would anyone know what this is called or how I can perform this using the new Unity UI elements? Thanks

Comment
Add comment · Show 1
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 RodrigoSeVeN · Mar 20, 2017 at 11:03 PM 0
Share

I don't have an answer, but I think this is what you're looking for: http://answers.unity3d.com/questions/388271/offscreen-effect.html

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Filhanteraren · Mar 21, 2017 at 12:43 AM

I made a simplified version of this tutorial here:

https://www.youtube.com/watch?v=gAQpR1GN0Os

Here is the script, its not perfect but should give you an idea on how it works

 using UnityEngine;
 using UnityEngine.UI;
 
 public class OffScreenIndicator : MonoBehaviour
 {
     [SerializeField] private Image _uiArrow;
     [SerializeField] private Image _uiIndicator;
     [SerializeField] private Transform _trackingObject;
 
     // Use this for initialization
     void Start()
     {
     }
 
     // Update is called once per frame
     void Update()
     {
         UpdateOffscrenIndicator(_trackingObject.position);
     }
 
 
     private void UpdateOffscrenIndicator(Vector3 position)
     {
         // Based on code from https://www.youtube.com/watch?v=gAQpR1GN0Os
         // youtube user digijin
 
         Vector3 screenpos = Camera.main.WorldToScreenPoint(position);
 
         if (screenpos.z > 0 && screenpos.x > 0 && screenpos.x < Screen.width && screenpos.y > 0 && screenpos.y < Screen.height)
         {
             // THE OBJECT IS INSIDE VIEW
             _uiIndicator.transform.position = screenpos;
             _uiIndicator.transform.rotation = Quaternion.Euler(0, 0, 0);
             _uiIndicator.enabled = true;
             _uiArrow.enabled = false;
         }
         else
         {
             // THE OBJECT IS OUTSIDE VIEW
             _uiIndicator.enabled = false;
             _uiArrow.enabled = true;
 
             Vector3 screenCenter = new Vector3(Screen.width, Screen.height, 0) / 2;
 
             screenpos -= screenCenter;
 
             if (screenpos.z < 0)
             {
                 screenpos *= -1;
             }
 
             float angle = Mathf.Atan2(screenpos.y, screenpos.x);
             angle -= 90 * Mathf.Deg2Rad;
 
             float cos = Mathf.Cos(angle);
             float sin = Mathf.Sin(angle);
 
             screenpos = screenCenter + new Vector3(sin * 150, cos * 150, 0);
 
             float m = cos / sin;
             Vector3 screenBounds = screenCenter * 1f;
 
             screenpos = cos > 0 ? new Vector3(-screenBounds.y / m, screenCenter.y, 0) : new Vector3(screenBounds.y / m, -screenCenter.y, 0);
 
             if (screenpos.x > screenBounds.x)
             {
                 screenpos = new Vector3(screenBounds.x, -screenBounds.x * m, 0);
             }
             else if (screenpos.x < -screenBounds.x)
             {
                 screenpos = new Vector3(-screenBounds.x, screenBounds.x * m, 0);
             }
 
             screenpos += screenCenter;
 
             _uiArrow.transform.position = screenpos;
             _uiArrow.transform.rotation = Quaternion.Euler(0, 0, angle * Mathf.Rad2Deg);
         }
     }
 }

Comment
Add comment · 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

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

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

Related Questions

Stack Overflow Error: I can't figure out why it is happening,Stack Overflow Error. Can't figure out why it is happening. 1 Answer

My game screen is getting blue for 1 or 2 seconds when i am requesting and getting response ? 0 Answers

Canvas without Camera? 2 Answers

How can I organize menus as a tree hierarchy? 0 Answers

Offset between mouse cursor position and "hit" position with worldspace UI's 1 Answer


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