Unity 如何用鼠标选中物体并随鼠标移动

0、演示

在这里插入图片描述

1、定义变量

    public Camera theCamera;                 //UI摄像机
    private GameObject Element;              //控件
    private bool clicked = false;            //判断是否点击过控件
    private bool is_element = false;         //是否有控件
    Vector3 newWorldPos;                     //新坐标

2、点击发出射线,判断是否命中tag为elelment的控件

        //按下左键开始发出射线
        if (!clicked && Input.GetMouseButtonDown(0))
        {
            Ray ray = theCamera.ScreenPointToRay(Input.mousePosition);                  //射线由主摄像机发出,射向屏幕点击的点
            RaycastHit hit;                                                             //射线撞击点
            if (Physics.Raycast(ray, out hit) && hit.collider.tag == "element")          //如果射线撞击到碰撞体,且碰撞体的标签是我们设置需要拖拽的物体,那么进行主逻辑
            {
                Element = hit.collider.gameObject;                                      //记录下拖拽物的原始屏幕空间位置
                Element.GetComponent<Collider>().enabled = false;                       //关闭该物体的Collider,使射线可以穿过
                is_element = true;
                clicked = true;
            }
        }

3、获取控件后,按住鼠标左键移动控件,移动的新位置为相机射线与tag为Plane或者element的控件碰撞的位置

        //左键一直处于按下状态,即为拖拽过程
        if (clicked && Input.GetMouseButton(0))
        {
            //如果拖拽物标记为element
            if (is_element)
            {
                Ray ray = theCamera.ScreenPointToRay(Input.mousePosition);                   //射线由主摄像机发出,射向屏幕点击的点
                RaycastHit hit;                                                              //射线撞击点
                if (Physics.Raycast(ray, out hit) && (hit.collider.tag == "Plane"|| hit.collider.tag == "element"))  //如果射线撞击到地面或者其他物体
                {
                    newWorldPos = hit.point;                                                 //新位置为射线撞击到地面的位置                        
                    newWorldPos.y = newWorldPos.y+1.5f;                                      //使物体抬起
                    Element.transform.position = newWorldPos;                                //将新位置位置赋予拖拽物
                }
            }
        }

4、松开左键,放下物体

        //松开左键
        if (clicked && Input.GetMouseButtonUp(0) )
        {
            
            newWorldPos.y = 0.77f;                                               //放下物体
            Element.transform.position = newWorldPos;                            //将世界空间位置赋予拖拽物
            Element.GetComponent<Collider>().enabled = true;                     //打开该物体的Collider,使射线不可以穿过
            is_element = false;
            clicked = false;
            Element = null;
        }

注意:要自己设置tag

5、完整代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//--------------------------------------------------------------------
//实现功能:点击物体后,将物体拖拽到相应位置
//位置更新:更新为主相机发出的射线与地面或其他物体碰撞的位置
public class move_controller2 : MonoBehaviour
{
    public Camera theCamera;                 //UI摄像机
    private GameObject Element;              //控件
    private bool clicked = false;            //判断是否点击过控件
    private bool is_element = false;         //是否有控件
    Vector3 newWorldPos;                     //新坐标
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        //按下左键开始发出射线
        if (!clicked && Input.GetMouseButtonDown(0))
        {
            Ray ray = theCamera.ScreenPointToRay(Input.mousePosition);                  //射线由主摄像机发出,射向屏幕点击的点
            RaycastHit hit;                                                             //射线撞击点
            if (Physics.Raycast(ray, out hit) && hit.collider.tag == "element")          //如果射线撞击到碰撞体,且碰撞体的标签是我们设置需要拖拽的物体,那么进行主逻辑
            {
                Element = hit.collider.gameObject;                                      //记录下拖拽物的原始屏幕空间位置
                Element.GetComponent<Collider>().enabled = false;                       //关闭该物体的Collider,使射线可以穿过
                is_element = true;
                clicked = true;
            }
        }
        //左键一直处于按下状态,即为拖拽过程
        if (clicked && Input.GetMouseButton(0))
        {
            //如果拖拽物标记为element
            if (is_element)
            {
                Ray ray = theCamera.ScreenPointToRay(Input.mousePosition);                   //射线由主摄像机发出,射向屏幕点击的点
                RaycastHit hit;                                                              //射线撞击点
                if (Physics.Raycast(ray, out hit) && (hit.collider.tag == "Plane"|| hit.collider.tag == "element"))  //如果射线撞击到地面或者其他物体
                {
                    newWorldPos = hit.point;                                                 //新位置为射线撞击到地面的位置                        
                    newWorldPos.y = newWorldPos.y+1.5f;                                      //使物体抬起
                    Element.transform.position = newWorldPos;                                //将新位置位置赋予拖拽物
                }
            }
        }
        //松开左键
        if (clicked && Input.GetMouseButtonUp(0) )
        {
            
            newWorldPos.y = 0.77f;                                               //放下物体
            Element.transform.position = newWorldPos;                            //将世界空间位置赋予拖拽物
            Element.GetComponent<Collider>().enabled = true;                     //打开该物体的Collider,使射线不可以穿过
            is_element = false;
            clicked = false;
            Element = null;
        }

    }
}

  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Unity中拾取物体的代码可以通过以下步骤实现: 1. 首先,在场景中创建一个物体,并将其添加一个Collider组件(如Box Collider、Sphere Collider等)。 2. 在拾取物体的脚本中添加以下代码: ```csharp public Transform player; public float pickUpDistance = 3f; private Collider coll; void Start() { // 获取物体的Collider组件 coll = GetComponent<Collider>(); } void Update() { // 判断玩家是否按下了拾取键(这里假设拾取键是E键) if (Input.GetKeyDown(KeyCode.E)) { // 计算玩家和物体之间的距离 float distance = Vector3.Distance(transform.position, player.position); // 判断玩家是否在物体的拾取范围内 if (distance <= pickUpDistance) { // 将物体的父级设置为玩家,使其跟随玩家移动 transform.parent = player; // 禁用物体的Collider组件,防止其他玩家再次拾取 coll.enabled = false; } } } ``` 在这个例子中,我们假设玩家的Transform组件已经被获取并存储在"player"变量中,"pickUpDistance"变量用于设置玩家可以拾取物体的最大距离。在Update()函数中,我们判断玩家是否按下了拾取键(E键),如果距离物体的距离小于等于"pickUpDistance",则将物体的父级设置为玩家,并禁用物体的Collider组件。 当玩家再次按下拾取键时,将物体的父级设置为null,使其恢复到原来的位置,并重新启用Collider组件,其他玩家就可以再次拾取这个物体了。 注意:在编写拾取物体的脚本时,需要确保物体和玩家都具有Rigidbody组件,否则可能会出现一些意想不到的问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值