Why is my arrow texture being drawn in odd places?

Posted by tyjkenn on Game Development See other posts from Game Development or by tyjkenn
Published on 2012-10-01T19:48:37Z Indexed on 2012/10/01 21:53 UTC
Read the original article Hit count: 267

Filed under:
|
|

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));
        }
    } 
}

© Game Development or respective owner

Related posts about unity

Related posts about textures