栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

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

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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().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().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().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().enabled = true;                     //打开该物体的Collider,使射线不可以穿过
            is_element = false;
            clicked = false;
            Element = null;
        }

    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/904991.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号