在unity3d中使用手指拖动3d对象 [英] Drag 3d object using fingers in unity3d
本文介绍了在unity3d中使用手指拖动3d对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建一个具有AR相机和3d对象的场景.当应用程序启动时,我在屏幕上看到该对象.我想用手指移动此对象.我尝试了很多代码.但是我找不到适合自己的解决方案.
I create a scene has an AR camera and a 3d object. When application starts, I see the object on the screen. I want to move this object using fingers. I tried many codes. But I couldn't find a nice solution for me.
AR摄像机被标记为MainCamera
.我使用下面的代码,结果对我来说是意外的.我单击对象,控制台输出在这里:
AR camera is tagged MainCamera
. I use below code, and the result is unexpected for me. I click on object and the console output is here:
如何将3d对象移动到鼠标单击的位置?我不使用任何标记.
How can I move 3d object to mouse click position? I don't use any marker.
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);
推荐答案
尝试以下代码:
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 ;
}
这篇关于在unity3d中使用手指拖动3d对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文