public Camera mCamera = null;
//设置物体距离屏幕距离
public float depth = 10f;
//类似于碰撞检测函数
void OnMouseEnter()
{
this.transform.localScale = new Vector3(1.3f, 1.3f, 1.3f);
}
void OnMouseExit()
{
this.transform.localScale = new Vector3(1f, 1f, 1f);
}
void OnMouseOver()
{
this.transform.Rotate(Vector3.up, 45 * Time.deltaTime, Space.Self);
}
void OnMouseDrag()
{
moveObject();
//设置物体与屏幕的距离
moveObject_fixdepth();
}
void moveObject()
{
Ray r = mCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
//1表示lay层,需要在unity中界面设置
if (Physics.Raycast(r, out hit, 1000f, 1))
{
this.transform.position = new Vector3(hit.point.x,
hit.point.y+0.5f,
hit.point.z);
//将打印的信息转化为一根红线
Debug.DrawLine(r.origin, hit.point, Color.red);
Unity利用鼠标拖拽物体及射线检测的利用
最新推荐文章于 2024-12-16 09:07:33 发布
