使用unity实现箭头贴屏滑动效果

使用unity实现箭头指向一物体 随屏幕移动贴屏滑动效果

直接上源码 有兴趣的同学可以试一试

using System.Collections;
using UnityEngine;

public class UIGuidManager : MonoBehaviour
{
    /// <summary>
    /// 箭头
    /// </summary>
    public Transform arrows;
    /// <summary>
    /// 指向目标物体
    /// </summary>
    Transform target;
    /// <summary>
    /// 原始位置
    /// </summary>
    Vector3 prePos = Vector3.zero;
    /// <summary>
    /// 检测时间
    /// </summary>
    public float checkTime = 0.2f;
    public void SetGuid(Transform _target)
    {
        target = _target;
    }
    void Start()
    {
        arrows.gameObject.SetActive(false);
        StartCoroutine(TrakerGuider());
    }

    private IEnumerator TrakerGuider()
    {
        while (true)
        {
            yield return new WaitForSeconds(checkTime);
            SetGuid();
        }
    }

    private void SetGuid()
    {
        if (target)
        {
            var pos = Camera.main.WorldToScreenPoint(target.transform.position);
            Debug.Log("pos:" + pos);
            var distance = Vector3.Distance(prePos, pos);
            if (distance < 1) { Debug.Log("沒動"); return; };
            prePos = pos;
            if (0 < pos.x && pos.x < Screen.width && pos.y > 0 && pos.y < Screen.height && pos.z > 0)
            {
                arrows.gameObject.SetActive(false);
                return;
            }
            else
                arrows.gameObject.SetActive(true);
            if (pos.z < 0)
                pos = pos * -1;
            var startpos = new Vector3(Screen.width / 2f, Screen.height / 2f, 0);
            var dir = pos - startpos;

            //通过反余弦函数获取 向量 a、b 夹角(默认为 弧度)
            float radians = Mathf.Atan2(dir.y, dir.x);

            //将弧度转换为 角度
            float angle = radians * Mathf.Rad2Deg;
            Debug.Log("angle:" + angle);
            arrows.localEulerAngles = new Vector3(0, 0, angle);
            // arrows.transform.LookAt(new Vector3(pos.x, pos.y, 0));
            float sereenangle = (float)Screen.height / (float)Screen.width;
            var va = System.Math.Abs(dir.y / dir.x);
            if (va <= sereenangle)
            {
                var length = arrows.GetComponent<RectTransform>().sizeDelta.x;
                if (pos.x < 0)
                    arrows.transform.position = GetNode(pos, startpos, length * 0.5f);
                else
                    arrows.transform.position = GetNode(pos, startpos, Screen.width - length * 0.5f);
            }
            else
            {
                var length = arrows.GetComponent<RectTransform>().sizeDelta.x;

                if (pos.y < 0)
                    arrows.transform.position = GetNode2(pos, startpos, length * 0.5f);
                else
                    arrows.transform.position = GetNode2(pos, startpos, Screen.height - length * 0.5f);
            }
        }
    }

    private Vector3 GetNode2(Vector3 pos, Vector3 startpos, float v)
    {
        pos = new Vector3(pos.x, pos.y, 0);
        Vector3 ab = pos - startpos;
        Vector3 am = ab * (Mathf.Abs(startpos.y - v) / Mathf.Abs(pos.y - startpos.y));
        Vector3 om = startpos + am;
        return om;
    }

    private Vector3 GetNode(Vector3 pos, Vector3 startpos, float v)
    {
        //float high = ((startpos.x - length) * (pos.y - startpos.y) / (startpos.x - pos.x)) + startpos.y;
        //return new Vector3(length, high, 0);
        pos = new Vector3(pos.x, pos.y, 0);
        Vector3 ab = pos - startpos;
        Vector3 am = ab * (Mathf.Abs(startpos.x - v) / Mathf.Abs(pos.x - startpos.x));
        Vector3 om = startpos + am;
        return om;
    }
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值