0
\$\begingroup\$

I have placed four gameobject at different place.And I have a player and have applied movement to it through keyboard control.I have added script for arrow indication towords this gameobject.But the problem is that,the arrow appeaar only when the user go near to the gameoobject.I need the arrows to appear on the screen so that the user initially know where the gameobject is placed on the scene.

Here is the code for onscreen arrow.I have attached this script to each gameobject

public Texture2D icon; //The icon. Preferably an arrow pointing upwards.
public float iconSize = 50f;
[HideInInspector]
public GUIStyle arrow; //GUIStyle to make the box around the icon invisible. Public so that everything has the default stats.
Vector2 indRange;
float scaleRes = Screen.width/200; //The width of the screen divided by 500. Will make the GUI automatically
//scale with varying resolutions.
Camera cam;
public 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;

    arrow.normal.textColor = new Vector4 (0, 0, 0, 0); //Makes the box around the icon invisible.

}

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,arrow);
        GUIUtility.RotateAroundPivot(0, indPos); //Rotates GUI back to the default so that GUI drawn after is not rotated.
    }
}

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

}

How can I make the arrows to appear on the edges of the screen

\$\endgroup\$
-2
\$\begingroup\$

using Mathf.Clamp() can clamp the indicators to the edge of the screen.

\$\endgroup\$
1
  • \$\begingroup\$ Could you add more information? We usually expect a bit more quality in our answers. How does this relate to the question? Perhaps you could give a working example? \$\endgroup\$
    – Gnemlock
    Jul 25 '17 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.