Unity中鼠标控制3D物体进行拖拽

轴方向上的移动

拖拽

1 、获取3D物体在世界坐标的位置转换屏幕坐标

 Camera.main.WorldToScreenPoint(dragBeforeGameObjPos)

2、鼠标在屏幕的坐标与物体在屏幕的坐标Z轴进行拟合

Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z))

3、获取3D物体与拟合出来的坐标的偏移量

 offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z));

4、通过向量投影得到移动坐标

 Vector3.Project(currentPosition, transform.right) + Vector3.Project(dragBeforeGameObjPos, transform.up) + Vector3.Project(dragBeforeGameObjPos, transform.forward);

5、上干货,附带手指控制,手指控制逻辑同鼠标一样

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PointAxis : MonoBehaviour
{
    public bool IsBeSelected;
    [SerializeField]
    private PointAxis_Type curPAType;

    private bool isMouseDrag = false;
    private Vector3 screenPosition;
    private Vector3 offset;
    private Vector3 dragBeforeGameObjPos;
    void Start()
    {

        //parentTransform = transform.parent;
        //mr = GetComponent<MeshRenderer>();
        switch (curPAType)
        {
            case PointAxis_Type.Axis_X:
                //colorNormal = ColorNormalX;
                break;
            case PointAxis_Type.Axis_Y:
                //colorNormal = ColorNormalY;
                break;
            case PointAxis_Type.Axis_Z:
                //colorNormal = ColorNormalZ;
                break;
        }

    }

    // Update is called once per frame
    void Update()
    {
#if (UNITY_ANDROID || UNITY_IPHONE)
         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
         {
            dragBeforeGameObjPos = transform.position;
            screenPosition = Camera.main.WorldToScreenPoint(dragBeforeGameObjPos);
            offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, screenPosition.z));
            isMouseDrag = true;
        }


         if (Input.touchCount > 0 &&Input.GetTouch(0).phase == TouchPhase.Moved)
        {
            if (isMouseDrag && IsBeSelected)
            {
                //Debug.Log("开始拖拽了");
                Vector3 currentScreenSpace = new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, screenPosition.z);
                Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenSpace) + offset;
               // float tempLength = Vector3.Distance(currentPosition, transform.position);
                Vector3 tempPos = Vector3.zero;
                switch (curPAType)
                {
                    case PointAxis_Type.Axis_X:
                        tempPos = Vector3.Project(currentPosition, transform.right) + Vector3.Project(dragBeforeGameObjPos, transform.up) + Vector3.Project(dragBeforeGameObjPos, transform.forward);
                        break;
                    case PointAxis_Type.Axis_Y:
                        tempPos = Vector3.Project(currentPosition, transform.up) + Vector3.Project(dragBeforeGameObjPos, transform.right) + Vector3.Project(dragBeforeGameObjPos, transform.forward);
                        break;
                    case PointAxis_Type.Axis_Z:
                        tempPos = Vector3.Project(currentPosition, transform.forward) + Vector3.Project(dragBeforeGameObjPos, transform.up) + Vector3.Project(dragBeforeGameObjPos, transform.right);
                        break;
                }
                transform.position = tempPos;
            }
        }
        

         if(Input.GetTouch(0).phase == TouchPhase.Ended)
            isMouseDrag = false;
#else

        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            dragBeforeGameObjPos = transform.position;
            screenPosition = Camera.main.WorldToScreenPoint(dragBeforeGameObjPos);
            offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z));
            isMouseDrag = true;
        }
        if (Input.GetKey(KeyCode.Mouse0))
        {
            if (isMouseDrag && IsBeSelected)
            {
                Vector3 currentScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z);
                Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenSpace) + offset;
                Vector3 tempPos = Vector3.zero;
                switch (curPAType)
                {
                    case PointAxis_Type.Axis_X:
                        tempPos = Vector3.Project(currentPosition, transform.right) + Vector3.Project(dragBeforeGameObjPos, transform.up) + Vector3.Project(dragBeforeGameObjPos, transform.forward);
                        break;
                    case PointAxis_Type.Axis_Y:
                        tempPos = Vector3.Project(currentPosition, transform.up) + Vector3.Project(dragBeforeGameObjPos, transform.right) + Vector3.Project(dragBeforeGameObjPos, transform.forward);
                        break;
                    case PointAxis_Type.Axis_Z:
                        tempPos = Vector3.Project(currentPosition, transform.forward) + Vector3.Project(dragBeforeGameObjPos, transform.up) + Vector3.Project(dragBeforeGameObjPos, transform.right);
                        break;
                }
                transform.position = tempPos;
            }
        }
        if (Input.GetKeyUp(KeyCode.Mouse0))
            isMouseDrag = false;
#endif

    }
}
public enum PointAxis_Type
{
    Axis_X, Axis_Y, Axis_Z
}

自由拖拽移动

自由拖拽

自由拖拽同上不同的是 获取3D物体与拟合出来的坐标的加上偏移量限制Y轴坐标就搞定了,

话不多说上干货

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PointEntity : MonoBehaviour
{

    public bool IsBeSelected = false;
    private bool isMouseDrag = false;
    private Vector3 screenPosition;
    private Vector3 offset;
    
    void Start()
    {
    }
    
    void Update()
    {
#if (UNITY_ANDROID || UNITY_IPHONE)
          if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
         {
             screenPosition = Camera.main.WorldToScreenPoint(gameObject.transform.position);
            offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, screenPosition.z));
            isMouseDrag = true;
        }


         if (Input.touchCount > 0 &&Input.GetTouch(0).phase == TouchPhase.Moved)
        {
            if (isMouseDrag && IsBeSelected)
            {
                //Debug.Log("开始拖拽了");
                Vector3 currentScreenSpace = new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, screenPosition.z);
                Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenSpace) + offset;
                currentPosition = new Vector3(currentPosition.x, transform.position.y, currentPosition.z);
                gameObject.transform.position = currentPosition;
            }
        }
        
         if(Input.GetTouch(0).phase == TouchPhase.Ended)
            isMouseDrag = false;
#else
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            screenPosition = Camera.main.WorldToScreenPoint(gameObject.transform.position);
            offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z));
            isMouseDrag = true;
        }
        if (Input.GetKey(KeyCode.Mouse0))
        {
            if (isMouseDrag && IsBeSelected)
            {
                //Debug.Log("开始拖拽了");
                Vector3 currentScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z);
                Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenSpace) + offset;
                currentPosition = new Vector3(currentPosition.x, transform.position.y, currentPosition.z);
                gameObject.transform.position = currentPosition;
            }
        }
        if (Input.GetKeyUp(KeyCode.Mouse0))
        {
            isMouseDrag = false;
        }
#endif
    }
}

  • 14
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w-白兰地

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值