美文网首页Unity基础入门分享
垂直于任意平面的拖动

垂直于任意平面的拖动

作者: 你的头好大 | 来源:发表于2017-11-24 17:51 被阅读11次

    毕竟重要的是VerticalPlane这个平面,你可以直接在Unity中创建一个Quad。然后拉到这个代码里面。
    VerticalPlane.transform.up的方向,就是垂直你拖动平面的方向。所以你可以调整你VerticalPlane的角度,可以比较直观的获得你所需要垂直拖动的平面。代码应该看看都能懂,比较简单。

      public class DragStuffBase : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
            {
    
                public Camera MainCamera;
                private float depthZ;
                public Vector3 InitPos;
                public bool isCanDrag = false;
                public float DepthZLerp = -0.3f;
                private bool drag;
    
                public Transform VerticalPlane;//垂直滑动的平面
    
    
    
                float VerticalLineLength;
                float distance;
    
                Vector3 originPointPos;
                Vector3 originObjPos;
    
                public Vector3 LerpPos=new Vector3(0,0.2f,0);//
    
                public void Init()
                {
                   
                    
                }
                // Use this for initialization
                void Start()
                {
                  
    
                }
    
                // Update is called once per frame
                void Update()
                {
    
                }
    
                public void OnBeginDrag(PointerEventData eventData)
                {
                    if (DOTween.IsTweening(this.transform))
                        return;
                    if (isCanDrag)
                    {
                        depthZ = this.MainCamera.WorldToScreenPoint(this.transform.position).z;
                        InitPos = this.transform.position;
                        drag = true;
    
                        distance = eventData.pointerPressRaycast.distance; ;
                        VerticalLineLength = GetVerticalLine(eventData.position, distance);
                        originPointPos = GetWorldPos(eventData.position, distance);
                        originObjPos = this.gameObject.transform.position+ LerpPos;
                        OnBeginDragEvent(eventData);
                    }
                }
    
                public void OnDrag(PointerEventData eventData)
                {
                    if (!drag)
                        return;
    
                    // this.transform.position = MainCamera.ScreenToWorldPoint(new Vector3(eventData.position.x, eventData.position.y, depthZ + DepthZLerp));
    
                    Vector3 tempPos = GetWorldPos(eventData.position, GetDragDistance(eventData.position));
                    Vector3 deltaPos = tempPos - originPointPos;
                    // this.transform.position = (originObjPos + deltaPos);
    
                    this.transform.position = tempPos + LerpPos;
    
                    OnDragEvent(eventData);
                }
    
                public void OnEndDrag(PointerEventData eventData)
                {
                    if (!drag)
                        return;
                    drag = false;
                    OnEndDragEvent(eventData);
                }
    
                public virtual void OnBeginDragEvent(PointerEventData eventData)
                {
    
                }
    
                public virtual void OnDragEvent(PointerEventData eventData)
                {
    
                }
    
                public virtual void OnEndDragEvent(PointerEventData eventData)
                {
    
                }
    
    
                float GetVerticalLine(Vector2 mousePos, float distance)
                {
                    Ray objRay = MainCamera.ScreenPointToRay(mousePos);
                    Ray vertical = new Ray(MainCamera.transform.position, new Vector3(VerticalPlane.transform.up.x, VerticalPlane.transform.up.y, VerticalPlane.transform.up.z));
                    Vector3 objDir = objRay.direction;
                    Vector3 verticalLineDir = vertical.direction;
    
                    float angle = Vector3.Angle(objDir, verticalLineDir);
    
                    return distance * Mathf.Cos(angle * Mathf.Deg2Rad);
                }
    
                float GetDragDistance(Vector2 mousePos)
                {
                    Ray vertical = new Ray(MainCamera.transform.position, new Vector3(VerticalPlane.transform.up.x, VerticalPlane.transform.up.y, VerticalPlane.transform.up.z));
                    Ray objRay = MainCamera.ScreenPointToRay(mousePos);
    
                    Vector3 objDir = objRay.direction;
                    Vector3 verticalLineDir = vertical.direction;
    
                    float angle = Vector3.Angle(objDir, verticalLineDir);
    
                    return VerticalLineLength / Mathf.Cos(angle * Mathf.Deg2Rad);
                }
    
                protected Vector3 GetWorldPos(Vector2 mousePos, float distance)
                {
                    Ray ray = MainCamera.ScreenPointToRay(mousePos);
                    return ray.GetPoint(distance);
                }
    
            }
    ···

    相关文章

      网友评论

        本文标题:垂直于任意平面的拖动

        本文链接:https://www.haomeiwen.com/subject/uipfbxtx.html