怪物攻击角色时,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;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值