怪物攻擊角色時,UI上箭頭執行怪物的方向

下面是插件的原碼:

using UnityEngine;

using System.Collections;

public class ArrowSet : MonoBehaviour
{
    public Texture arrow;
    private Camera cam;
    private float xCenter;
    private float yCenter;
    private float screenSlope;
    private float halfSize;
    public float size = 30;
    private bool errorless = false;
    public string tagToFind = "Enemy";
    public float hoverAngle = 270;
    public float distanceAbove = 30;
    public float blindSpot = 5.0f;
    public bool hoverOnScreen = true;
    void Start()
    {
        Camera theCam = gameObject.GetComponent<Camera>();
        if (gameObject.GetComponent<Camera>())
        {
            cam = theCam;
            if (arrow != null)
            {
                errorless = true;
            }
            else
            {
                Debug.Log("請附上一個箭頭紋理雷達箭腳本。");
                errorless = false;
            }
        }
        else
        {
            Debug.Log("雷達箭頭必須連接到一個攝像機。");
        }
    }
    void OnGUI()
    {
        if (Event.current.type.Equals(EventType.Repaint) && errorless)
        {


            xCenter = cam.pixelWidth / 2;
            yCenter = cam.pixelHeight / 2;
            screenSlope = ((float)cam.pixelHeight) / cam.pixelWidth;
            halfSize = size / 2;
            GameObject[] objects = GameObject.FindGameObjectsWithTag(tagToFind);
            foreach (GameObject ob in objects)
            {
                float angle = hoverAngle - 180;
                double rad = angle * (Mathf.PI / 180.0);
                Vector3 arrowPos = cam.transform.right * Mathf.Cos((float)rad) + cam.transform.up * Mathf.Sin((float)rad);
                Vector3 worldPos = ob.transform.position + (arrowPos * distanceAbove);
                //世界轉視口 
                Vector3 pos = cam.WorldToViewportPoint(worldPos);
                //Debug.Log(pos);
                if (pos.z < 0)
                {
                    pos.x *= -1;
                    pos.y *= -1;
                }
                if (pos.z > 0 || (pos.z < 0 && (pos.x > 0.5f + (blindSpot / 2) || pos.x < 0.5f - (blindSpot / 2)) && (pos.y < 0.5f - (blindSpot / 2) || pos.y > 0.5f + (blindSpot / 2))))
                {
                    //找到新的位置
                    float newX = pos.x * cam.pixelWidth;
                    //相機座標垂直翻轉相比,像素座標
                    float newY = cam.pixelHeight - pos.y * cam.pixelHeight;
                    ////如果對象是屏幕外
                    if (pos.z < 0 || newY < 0 || newY > cam.pixelHeight || newX < 0 || newX > cam.pixelWidth)
                    {
                        float a = CalculateAngle(cam.pixelWidth / 2, cam.pixelHeight / 2, newX, newY);
                        Vector2 coord = ProjectToEdge(newX, newY);
                        GUIUtility.RotateAroundPivot(a, coord);
                        Graphics.DrawTexture(new Rect(coord.x - halfSize, coord.y - halfSize, size, size), arrow);
                        GUIUtility.RotateAroundPivot(-a, coord);
                    }
                    else
                        if (hoverOnScreen)
                        {
                            Debug.Log(222);
                            float nh = Mathf.Sin((float)rad) * size;
                            float nw = Mathf.Cos((float)rad) * size;
                            //當在屏幕上,只需旋轉90度,並繪製
                            GUIUtility.RotateAroundPivot(-angle + 180, new Vector2(newX + nw, newY - nh));
                            Graphics.DrawTexture(new Rect(newX + nw, newY - nh - halfSize, size, size), arrow, null);
                            GUIUtility.RotateAroundPivot(angle - 180, new Vector2(newX + nw, newY - nh));
                        }
                }
            }
        }
    }


    float CalculateAngle(float x1, float y1, float x2, float y2)
    {
        float xDiff = x2 - x1;
        float yDiff = y2 - y1;
        float rad = Mathf.Atan(yDiff / xDiff);
        float deg = rad * 180 / Mathf.PI;
        if (xDiff < 0)
        {
            deg += 180;
        }
        return deg;
    }
    Vector2 ProjectToEdge(float x2, float y2)
    {
        float xDiff = x2 - (cam.pixelWidth / 2);
        float yDiff = y2 - (cam.pixelHeight / 2);
        float slope = yDiff / xDiff;
        Vector2 coord = new Vector2(0, 0);
        float ratio;
        if (slope > screenSlope || slope < -screenSlope)
        {
            //project on top/bottom
            ratio = (yCenter - halfSize) / yDiff;
            if (yDiff < 0)
            {
                coord.y = halfSize;
                ratio *= -1;
            }
            else coord.y = cam.pixelHeight - halfSize;
            coord.x = xCenter + xDiff * ratio;
        }
        else
        {
            //project on left/right
            ratio = (xCenter - halfSize) / xDiff;
            if (xDiff < 0)
            {
                coord.x = halfSize;
                ratio *= -1;
            }
            else coord.x = cam.pixelWidth - halfSize;
            coord.y = yCenter + yDiff * ratio;
        }
        return coord;
    }

}


以前代碼在 Unity4.6運行良好,但在Untiy5以後的版本就有BUG,上下方向的怪物好使,左右的不顯示UI,經過查找發現Camera.pixelWidth和Camera.pixelHeight不再是float值,而是int類型,我們之需要將它們轉成flaot就可以修復這個BUG 

screenSlope = ((float)cam.pixelHeight) / cam.pixelWidth;

發佈了23 篇原創文章 · 獲贊 2 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章