我创建了一个具有AR摄像头和3d对象的场景。应用程序启动时,我会在屏幕上看到该对象。我想用手指移动这个物体。我尝试了很多代码。但我找不到一个很好的解决方案。
AR相机已标记为MainCamera
。我使用下面的代码,结果对我来说意外。我点击对象,控制台输出在这里:
如何将3d对象移动到鼠标点击位置?我不使用任何标记。
Vector3 vect3 = Camera.main.WorldToScreenPoint(car.transform.position);
Debug.Log("Vect3 = " + car.transform.position.x + "-" + car.transform.position.y + "-" + car.transform.position.z);
Vector3 p = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10.0f));//Camera.main.nearClipPlane));
Debug.Log("Mouse = " + p.x + "-" + p.y + "-" + p.z);
答案 0 :(得分:0)
试试这段代码:
private Vector3 scrPt;
private Vector3 distance;
void OnMouseDown()
{
scrPt= Camera.main.WorldToScreenPoint(car.transform.position);
distance = car.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void OnMouseDrag()
{
Vector3 currentScrPt= new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScrPt) + distance;
car.position = currentPosition ;
}