我已经找到了用于Unity 2D的对象拖动脚本。我在互联网上找到了一个很好的方法,但它似乎只是在Unity 3D中工作。这对我来说并不好,因为我正在制作一款2D游戏,并且不会以这种方式与“墙壁”相撞。在Unity 2D中拖动对象
我试图将其重写为2D,但我已经崩溃到与向量的错误。
如果你能帮我把它改写成2D,我会很高兴。
这里是什么工作在3D代码:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(BoxCollider))]
public class Drag : MonoBehaviour {
private Vector3 screenPoint;
private Vector3 offset;
void OnMouseDown() {
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void OnMouseDrag()
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
}
}