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;
}
}
}