一开始,我用raycast和用getmousebutton一直出现拖动不成功的现象,是因为2D物理系统和3D是互相平行互不影响的。更改碰撞体后代码可用。
不过因为getmousebutton这样写会把剩下的碰撞判定全盘交给raycast,拖拽必须要在鼠标停留在碰撞体内才可以实现,而且由于刷新位置的次数依赖于update,拖拽功能会出现延迟现象,因为帧刷新位置跟不上鼠标移动的速度,所以推荐2D拖拽还是要用接口也就是OnMouseDrag,不要用button,很累,
修改前
public class mouseattain : MonoBehaviour
{
public Texture2D cursorTexture;
public CursorMode cursorMode = CursorMode.Auto;
public Vector2 hotSpot = Vector2.zero;
private Camera cam;
private GameObject go;
public static string btnName;
private Vector3 screenSpace;
private Vector3 offset;
private bool isDrage = false;
private bool p = true ;
private bool combine = false;
// Use this for initialization
void Start()
{
cam = Camera.main;
Cursor.SetCursor(cursorTexture, hotSpot, cursorMode);//改变鼠标样式
}
// Update is called once per frame
void FixedUpdate()
{
//创建一条射线,产生的射线是在世界空间中,从相机的近裁剪面开始并穿过屏幕position(x,y)像素坐标(position.z被忽略)
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
// Debug.Log(ray.ToString());
//RaycastHit是一个结构体对象,用来储存射线返回的信息
RaycastHit hitInfo;
if (Input.GetMouseButtonDown(0))
{
isDrage = true;
}
if (Input.GetMouseButtonUp(0))
{
isDrage = false;
combine = false;
}
if (isDrage==true)
{
if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity))
{
combine = true;
}
if (combine==true)
{
Debug.Log(p);
Debug.Log(isDrage);
Debug.DrawLine(ray.origin, hitInfo.point);
go = hitInfo.collider.gameObject;
Vector3 currentScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
Vector3 currentPosition = cam.ScreenToWorldPoint(currentScreenSpace) + offset;
if (btnName != null)
{
go.transform.position = currentPosition;
}
screenSpace = cam.WorldToScreenPoint(go.transform.position);
offset = go.transform.position - cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
//offset为go物体和摄像机之间在世界中的偏移量
btnName = go.name;
}
else
{
btnName = null;
}
}
bool IfCombine(bool combine)
{
if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity))
{
combine = true;
}
}
}
修改以后
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mousetest : MonoBehaviour
{
private Vector3 point;
private Camera camera;
bool memory = false;
private Camera cam;
private GameObject go;
public static string btnName;
private Vector3 screenSpace;
private Vector3 offset;
private bool found = false;
private bool combine = false;
Vector3 currentScreenSpace;
Vector3 currentPosition;
// Use this for initialization
void Start()
{
cam = Camera.main;
}
// Update is called once per frame
void Update()
{
currentScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
currentPosition = cam.ScreenToWorldPoint(currentScreenSpace) + offset;
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
// Debug.Log(ray.ToString());
//RaycastHit是一个结构体对象,用来储存射线返回的信息
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity))
{
Debug.Log("object touched");
Debug.DrawLine(ray.origin, hitInfo.point);
go = hitInfo.collider.gameObject;
Debug.Log(go.transform.position);
screenSpace = cam.WorldToScreenPoint(go.transform.position);
offset = go.transform.position - cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
btnName = go.name;
found = true;
}
}
void OnMouseDrag()
{
if (found=true)
{
if (btnName != null)
{
go.transform.position = currentPosition;
//offset为go物体和摄像机之间在世界中的偏移量
}
}
else
{
btnName = null;
}
}
}