1
\$\begingroup\$

I am working on indicating on screen arrow to point towards the enemy. I have a environment which consist of a player and three enemy placed at different position on the environment.I need an arrow indicating the enemy,so that the player can move towards the enemy in the environment.

How can I do this?Can anybody please help me out.

I have got a script on searching.

public Texture2D icon; //The icon. Preferably an arrow pointing upwards.
public float iconSize = 50f;
[HideInInspector]
public GUIStyle gooey; //GUIStyle to make the box around the icon invisible. Public so that everything has the default stats.
Vector2 indRange;
float scaleRes = Screen.width / 500; //The width of the screen divided by 500. Will make the GUI automatically
//scale with varying resolutions.
Camera cam;
bool visible = false; //Whether or not the object is visible in the camera.

void Start () {
    visible = GetComponent<SpriteRenderer> ().isVisible;

    cam = Camera.main; //Don't use Camera.main in a looping method, its very slow, as Camera.main actually
    //does a GameObject.Find for an object tagged with MainCamera.

    indRange.x = Screen.width - (Screen.width / 6);
    indRange.y = Screen.height - (Screen.height / 7);
    indRange /= 2f;

    gooey.normal.textColor = new Vector4 (0, 0, 0, 0); //Makes the box around the icon invisible.
    visible=false;
}

void OnGUI () {
    if (visible) {
        Vector3 dir = transform.position - cam.transform.position;
        dir = Vector3.Normalize (dir);
        dir.y *= -1f;

        Vector2 indPos = new Vector2 (indRange.x * dir.x, indRange.y * dir.y);
        indPos = new Vector2 ((Screen.width / 2) + indPos.x,
            (Screen.height / 2) + indPos.y);

        Vector3 pdir = transform.position - cam.ScreenToWorldPoint(new Vector3(indPos.x, indPos.y,
            transform.position.z));
        pdir = Vector3.Normalize(pdir);

        float angle = Mathf.Atan2(pdir.x, pdir.y) * Mathf.Rad2Deg;

        GUIUtility.RotateAroundPivot(angle, indPos); //Rotates the GUI. Only rotates GUI drawn after the rotate is called, not before.
        GUI.Box (new Rect (indPos.x, indPos.y, scaleRes * iconSize, scaleRes * iconSize), icon,gooey);
        GUIUtility.RotateAroundPivot(0, indPos); //Rotates GUI back to the default so that GUI drawn after is not rotated.
    }
}

void OnBecameInvisible() {
    visible = false;
}
//Turns off the indicator if object is onscreen.
void OnBecameVisible() {
    visible = true;
}

}

The problem is start when I play the scene the arrow is seen at the start but its get invisible soon

\$\endgroup\$
0
\$\begingroup\$

Not sure if it is the best option but what I would do is:

  • initialize an image arrow.png loaded in a variable
  • get currently selected enemy position x,y
  • get your position x2,y2
  • get the angle angle1 between you and the enemy with Vector2.Angle method
  • draw your arrow at (x+x2)/2 , (y+y2)/2 rotated with the angle1 you got

I do not know Unity and it is possible that there is a class which already cover this.

\$\endgroup\$
6
  • \$\begingroup\$ When you log/println the values for indPos.x and .y, they are always in the camera view box ? Also, could you println when the method isBecameInvisible is called? It is possible that the event triggering this method is not working properly. \$\endgroup\$
    – Matteo
    Oct 5 '16 at 14:47
  • \$\begingroup\$ I have a issue.When I take the build of the project,its arrow mark is not getting appeared on the instead.When we move the player the arrow get appeared?what will be the reason \$\endgroup\$ Oct 7 '16 at 7:59
  • \$\begingroup\$ Could you just log the values of visible from start to the end ? \$\endgroup\$
    – Matteo
    Oct 7 '16 at 8:10
  • \$\begingroup\$ yes Will log the value \$\endgroup\$ Oct 7 '16 at 8:10
  • 1
    \$\begingroup\$ after changing the value of visible to "public bool visible = false;" and OnGUI() { if(!visible) to if(visible) \$\endgroup\$ Oct 7 '16 at 8:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .