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

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

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

using System.Collections;
using UnityEngine;

public class UIGuidManager : MonoBehaviour
{
    /// 
    /// 箭头
    /// 
    public Transform arrows;
    /// 
    /// 指向目标物体
    /// 
    Transform target;
    /// 
    /// 原始位置
    /// 
    Vector3 prePos = Vector3.zero;
    /// 
    /// 检测时间
    /// 
    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;
    }
}

你可能感兴趣的:(使用unity实现箭头贴屏滑动效果)