Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

This is a script I wrote that places an arrow on the screen, pointing to an enemy off-screen, or, if the enemy is on-screen, it places an arrow hovering above the enemy. Everything seems to work, except for some odd reason, I see random arrows floating around, often skewed and resized (which I really don't understand, because I only rotate and place in this script). Even when I only have one enemy in the scene, I still see these random arrows. It should only be drawing one per enemy. Note: when all enemies are removed, no arrows appear.

var arrow : Texture;
var cam : Camera;
var dim : int = 30;

function OnGUI() {
    var objects = GameObject.FindGameObjectsWithTag("Enemy");
    for(var ob : GameObject in objects) {
        var pos = cam.WorldToViewportPoint(ob.transform.position);
        if(gameObject.GetComponent(FollowCamera).target != null){
            var tar = gameObject.GetComponent(FollowCamera).target.parent;
        }
        if(pos.z>1 && ob.transform != tar){
            var xDiff = (pos.x*cam.pixelWidth)-(cam.pixelWidth/2);
            var yDiff = (pos.y*cam.pixelHeight)-(cam.pixelHeight/2);
            var angle = Mathf.Rad2Deg*Mathf.Atan(yDiff/xDiff)+180;
            if(xDiff>0) angle += 180;

            var dist = Mathf.Sqrt(xDiff*xDiff + yDiff*yDiff);
            var slope = yDiff/xDiff;
            var camSlope = cam.pixelHeight/cam.pixelWidth;
            var theX = -1000.0;
            var theY = -1000.0;
            var mult = 0;
            var temp;
            if(Mathf.Abs(xDiff)>(cam.pixelWidth/2)||Mathf.Abs(yDiff)>(cam.pixelHeight/2)){
                //touching right
                if(slope<camSlope && slope>-camSlope) {
                    if(xDiff>(cam.pixelWidth/2)) {
                        theX = cam.pixelWidth - (dim/2);
                        mult = -1;
                    }else if(xDiff<-(cam.pixelWidth/2)) {
                        theX = (dim/2);
                        mult = 1;
                    }
                    temp = ((cam.pixelWidth/2)*yDiff)/xDiff;
                    theY =(cam.pixelHeight/2)+(mult*temp);
                } else{
                    if(yDiff>(cam.pixelHeight/2)) {
                        theY = (dim/2);
                        mult = 1;
                    }else if(yDiff<-(cam.pixelHeight/2)) {
                        theY = cam.pixelHeight - (dim/2);
                        mult = -1;
                    }
                    temp = ((cam.pixelHeight/2)*xDiff)/yDiff;
                    theX =(cam.pixelWidth/2)+(mult*temp);
                }
            } else {
                angle = -90;
                theX = (cam.pixelWidth/2)+xDiff;
                theY = (cam.pixelHeight/2)-yDiff-dim;
            }
            GUIUtility.RotateAroundPivot(-angle, Vector2(theX, theY));
            Graphics.DrawTexture(Rect(theX-(dim/2),theY-(dim/2),dim,dim),arrow,null);
            GUIUtility.RotateAroundPivot(angle, Vector2(theX, theY));
        }
    } 
}
share|improve this question
Are you using multiple cameras? You may want to render your lines in a specific layer and set the cull flag of the camera. – Rubber Mallet Oct 1 '12 at 20:29
No, only one camera. After creating thousands of enemies, though, I realized all the extra arrows appear to be along the same 3D plane, the way they are skewed. – tyjkenn Oct 1 '12 at 22:13

closed as too localized by Byte56, bummzack, Sean Middleditch, Trevor Powell, Gajoo Mar 15 at 23:30

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

Browse other questions tagged or ask your own question.