- Home /
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
please specify what you exactly want? Some kind of $$anonymous$$i map? Do you have a screenshot?
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:
Thanks so much Deequation, you're a genious. I've marked it as correct :D
Your answer
![](https://koobas.hobune.stream/wayback/20220613125343im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how do i Disable and Enable buttons? 2 Answers
How do I make an enemy recoil when hit? 1 Answer
Level Button And Quit Button Android 2 Answers
Enemy speed calculating 0 Answers
Clearing a GUI and Pausing a Game 1 Answer