• 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 sillanstudios · Jan 02, 2014 at 02:26 PM · androidguigameenemylocation

Enemy locators

Hi Guys,

Does anyone know how to do those locators to show what direction an enemy is coming in? Like the ones in Dead trigger 2. Any ideas or links or suggestions appreciated :D

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 HappyMoo · Jan 02, 2014 at 05:06 PM 0
Share

please specify what you exactly want? Some kind of $$anonymous$$i map? Do you have a screenshot?

1 Reply

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

Answer by Deequation · Jan 02, 2014 at 06:22 PM

If you meant; Either getting a arrow direction from your character, towards the enemy, or, getting an icon at the egde of the screen to where the enemy is, then it's pretty simple. To get the direction between you, and an enemy, assuming there is only one, but obviously you need to loop and re-do for all enemies, see example 01. To create an icon at the egde of the screen, in the direction of the enemy, see example 03.

Example 01: [Direction]

 public var Enemy    : Transform;
 
 function Update ()
 {
     // Step 1:
     // To get the arrow (vector) direction pointing FROM you, TO the enemy ..
     
     var Direction_A : Vector3  = Enemy.position - transform.position;
     
     // Step 2:
     // Draw a debug in the editor, to show this direction ..
     
     Debug.DrawRay(transform.position, Direction_A);
 }

Note that: In this example I just created a variable, for the enemy gameobject. You need to either get that by reference, register it on awake, and obviously loop through the entire array of enemies to get all directions. Also this method just shows a debug draw line, if you wanted it ingame, you had to use some drawing, texture, or similar. I don't feel that is worth posting for your questions nature. Also note, that this vector is the exact length between the enemy and the player, to make it a fixed length use this method:

Example 02: [Fixed length]

 Debug.DrawRay(transform.position, Direction_A.normalized * 2);

Example 03: [GUI draw]

 public var Enemy    : Transform;
 
 function OnGUI ()
 {
     var myScreenPosition : Vector3 = Camera.main.WorldToScreenPoint(Enemy.position);
     
     GUI.Box(Rect(myScreenPosition.x - 15, Screen.height - myScreenPosition.y - 15, 30, 30), " ");
 }

Note that in the last example, I just do a simple conversion from world space to screen coordinates, then using some math and logic, places the box at the right place. This will draw a (in this example) simple gui box on the enemy. Now, what I suspect you wanted, was more like when he was OUTSIDE the screen. This can easily be done by simple clamping the icons position to always be within x = [0, Screen.width], and y = [0, Screen.height] using the Mathf.Clamp(value,min,max). That way, when this will work like an icon in the side of the screen, showing an enemy is in THAT direction. Note that all of this can be done using the vector approach too, but ya ...

Related documentation:

  • http://docs.unity3d.com/Documentation/ScriptReference/Debug.DrawRay.html

  • http://docs.unity3d.com/Documentation/ScriptReference/Camera.WorldToScreenPoint.html

  • http://docs.unity3d.com/Documentation/ScriptReference/Mathf.Clamp.html

Comment
Add comment · Show 2 · 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 HappyMoo · Jan 02, 2014 at 07:26 PM 0
Share

thumbed up :D

avatar image sillanstudios · Jan 02, 2014 at 09:30 PM 0
Share

Thanks so much Deequation, you're a genious. I've marked it as correct :D

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

How do I make an enemy recoil when hit? 1 Answer

how do i Disable and Enable buttons? 2 Answers

Level Button And Quit Button Android 2 Answers

How to let the player save progress 1 Answer

GUI button doesn't appear on Android 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