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
1
Question by O-R-C · Feb 16, 2014 at 05:01 AM · guienemyfollow

GUI follow enemy object

The above says it all. I am looking to have a GUI above a enemy object, so that my character camera can see enemies from far away. I am relatively new to working with the GUI functions so could someone please point me in the right direction. Thanks

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
1
Best Answer

Answer by robertbu · Feb 16, 2014 at 05:15 AM

Putting something from the GUI class such as using GUI.DrawTexture() above a world object is a bit complicated...partly because you have to make two coordinate changes, and partly because GUI.* stuff is anchored in the upper left corner. Let me suggest instead that you use either a GUITexture or a GUIText object. Both are created from the 'GameObject > Create Other' menu. GUITextures and GUIText object live in Viewport space. So we can use a script like this to track and object:

 #pragma strict

 var target : Transform;
 var offset = Vector3(0.0, 0.75, 0.0);
 
 function Update() {
     var pos = target.position + offset;
     transform.position = Camera.main.WorldToViewportPoint(pos);
 }

You attach this script to a GUITexture. Then drag and drop the game object you want to follow on the 'target' variable.

Note when making the GUITexture, you may want to edit the Pixel Offset to set the 'Y' value to 0.0. This will anchor the GUITexture by the bottom.

Comment
Add comment · Show 5 · 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 O-R-C · Feb 16, 2014 at 07:22 AM 0
Share

Thank you this seems to work. I do have a problem though. The GUI appears where the object is, but when i turn away from the object you can still see it. Why would this be?

Thanks for the reply. This is the closest that I have got so far.

avatar image robertbu · Feb 16, 2014 at 07:28 AM 0
Share

What is 'turn away' mean? If you mean turn the camera, then I cannot reproduce it. That is, if the texture follows the object so that when I turn away from the object, I no longer see the GUITexture. If you mean the object turns away, then that is how GUITextures work. If you want something to track the object and also turn with the object, then you will need to use a game object that lives in world space like a Quad.

avatar image O-R-C · Feb 16, 2014 at 11:09 AM -1
Share

Sorry about that. I am making a game where you fly around in an aircraft. I would like the GUI to be over an enemy aircraft. What i meant when is said "turned way" is when my plane including the camera turned away from the target. the GUI stays on the screen, even when enemy is behind me.

avatar image robertbu · Feb 16, 2014 at 05:12 PM 0
Share

For a viewport coordinate, the object is in front of the camera if the 'z' is positive and behind it is negative. So you can solve this problem by adding one additional line of code:

 #pragma strict
 
 var target : Transform;
 var offset = Vector3(0.0, 0.75, 0.0);
 
 function Update() {
     var pos = target.position + offset;
     transform.position = Camera.main.WorldToViewportPoint(pos);
     guiTexture.enabled = (transform.position.z > 0.0);
 }
avatar image O-R-C · Feb 16, 2014 at 09:56 PM -1
Share

Thank you so much this is exactly what i wanted. Top marks to 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

GUI Follow RaycastHit 2 Answers

AI enemy Stuck at corners 2 Answers

Enemy's gather around the player.. How?! 2 Answers

Enemy follows player then attacks! 2 Answers

How to make an enemy stalk you 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