unity 屏幕追踪显示目标

    public Transform Target;  //追踪目标
    public RectTransform TeammatesObj;  //悬浮图标
    private Vector2 player2DPosition;
    private Vector2 TeammatesFrame = new Vector2(Screen.width * 0.5f - (Screen.height * 0.07f), Screen.height * 0.43f);   //显示图标在窗口边缘的位置

IEnumerator TeammatesIcon()
    { //悬浮边框图标显示队友位置

            //获得屏幕中心坐标
            Vector2 sc = new Vector2(Screen.width, Screen.height) * 0.5f;
        while (true)
        {
            player2DPosition = Camera.main.WorldToScreenPoint(Target.position);
            if (player2DPosition.x > Screen.width - 20 | player2DPosition.x < +20 | player2DPosition.y > Screen.height - 20 | player2DPosition.y < +20)   //只有目标在屏幕之外才会显示
            {
                Vector2 xy = Teammates2DPosition - sc;
                float x = 0;
                float y = 0;
                float a = Vector2.Angle(Vector2.right, new Vector2(Mathf.Abs(xy.x), Mathf.Abs(xy.y)));
                if (a < Vector2.Angle(Vector2.right, new Vector2(TeammatesFrame.x, TeammatesFrame.y)))
                {
                    x = TeammatesFrame.x;
                    y = Mathf.Tan(a * Mathf.PI / 180) * TeammatesFrame.x;
                }
                else
                {
                    x = Mathf.Tan((90 - a) * Mathf.PI / 180) * TeammatesFrame.y;
                    y = TeammatesFrame.y;
                }
                if (xy.x < 0)
                {
                    x = -x;
                }
                if (xy.z < 0)
                {
                    y = -y;
                }
                TeammatesObj.localPosition= new Vector3(x, y,-1 );  获得悬浮窗位置
            }
            yield return new WaitForSeconds(0.1f);
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值