Unity鼠标拖动3D物体移动,并限制拖动范围

using UnityEngine;
using System.Collections;

public class DragObject : MonoBehaviour
{
    /// <summary>
    /// 将要拖动的物体
    /// </summary>
    private Transform _dragGameObject;

    /// <summary>
    /// 获取射线需要碰撞的层
    /// </summary>
    private LayerMask _canDrag;

    /// <summary>
    /// 直接从外部定义好层,简单理解
    /// </summary>
    public LayerMask canDrag2;

    /// <summary>
    /// 获得鼠标的位置和cube位置差
    /// </summary>
    private Vector3 _offset;

    /// <summary>
    /// 是否点击到cube
    /// </summary>
    private bool _isClickCube;

    /// <summary>
    /// 目标对象的屏幕坐标
    /// </summary>
    private Vector3 _targetScreenPoint;

    //限制拖动范围的最小值和最大值
    public float xMin, xMax, zMin, zMax;

    // Use this for initialization
    private void Start()
    {
        _canDrag = 1 << LayerMask.NameToLayer("Player");
    }

    // Update is called once per frame
    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (CheckGameObject())
            {
                _offset = _dragGameObject.transform.position -
                          Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y,
                              _targetScreenPoint.z));
            }
        }

        if (_isClickCube)
        {
            //当前鼠标所在的屏幕坐标
            Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, _targetScreenPoint.z);
            //把当前鼠标的屏幕坐标转换成世界坐标
            Vector3 curWorldPoint = Camera.main.ScreenToWorldPoint(curScreenPoint);
            _dragGameObject.position = curWorldPoint + _offset;

            //限制拖动范围
            _dragGameObject.GetComponent<Rigidbody>().transform.position = new Vector3(
                Mathf.Clamp(_dragGameObject.GetComponent<Rigidbody>().transform.position.x, xMin, xMax),
                1.0f,
                Mathf.Clamp(_dragGameObject.GetComponent<Rigidbody>().transform.position.z, zMin, zMax)
            );
        }

        if (Input.GetMouseButtonUp(0))
        {
            _isClickCube = false;
            //恢复拖拽物体的Y轴为原点
            _dragGameObject.GetComponent<Rigidbody>().transform.position = new Vector3(
                _dragGameObject.GetComponent<Rigidbody>().transform.position.x, 0,
                _dragGameObject.GetComponent<Rigidbody>().transform.position.z);
        }
    }

    /// <summary>
    /// 检查是否点击到cbue
    /// </summary>
    /// <returns></returns>
    private bool CheckGameObject()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitInfo;
        if (Physics.Raycast(ray, out hitInfo, 100f, _canDrag))
        {
            _isClickCube = true;
            //得到射线碰撞到的物体
            _dragGameObject = hitInfo.collider.gameObject.transform;
            _targetScreenPoint = Camera.main.WorldToScreenPoint(_dragGameObject.position);

            return true;
        }

        return false;
    }
}

把该脚本挂到相机上,还有把需要拖动的物体的Layer修改成“Player”即可。没有Player的添加一下

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值