I have an AR scene that has one AR camera, an image target and 3d object below as.
我有一个AR场景,有一个AR摄像头,一个图像目标和下面的3d对象。
I create an .cs
file and attach to ARCamera. I want to move AR object to mouse click position. I tried many codes for this. But I couldn't success it.
我创建一个.cs文件并附加到ARCamera。我想将AR对象移动到鼠标单击位置。我尝试了很多代码。但我无法成功。
I know that Input.mouseposition
returns screen position. I convert to ScreenToWorldPosition
and put 3d object on this position. 3d object is moved, but not mouse click position. I don't know where it is moved.
我知道Input.mouseposition返回屏幕位置。我转换为ScreenToWorldPosition并将3d对象放在此位置。移动3d对象,但不移动鼠标单击位置。我不知道它在哪里移动。
How can I move to mouse click position? My code is here :
如何移动到鼠标点击位置?我的代码在这里:
Camera cam;
Vector3 target = new Vector3(0.0f, 10f,0.5f);
// Use this for initialization
void Start () {
if (cam == null)
cam = Camera.main;
}
void Update()
{
if (Input.GetMouseButtonDown(0)) {
Debug.Log("MouseDown");
Vector3 mousePos = Input.mousePosition;
mousePos = cam.ScreenToWorldPoint(mousePos);
GameObject.Find("Car1").gameObject.transform.position = mousePos;
}
}
EDIT 1 If I add a plane to scene, I can move to the position of mouse click only on the plane. The code is taken from here. But the plane prevents to show AR camera view. The screenshot is below:
编辑1如果我向场景添加平面,我可以移动到仅在平面上鼠标点击的位置。代码取自这里。但飞机无法显示AR摄像机视图。截图如下:
2 个解决方案
#1
0
Instead of playing with ScreenToWorldPoint
, you should use a raycast against a plane, see this answer: https://*.com/a/29754194/785171.
您应该使用针对飞机的光线投射,而不是使用ScreenToWorldPoint,请参阅以下答案:https://*.com/a/29754194/785171。
The plane should be attached to your AR marker.
飞机应该连接到您的AR标记。
#2
0
ScreenToWorldPoint
takes a Vector3
, mousePosition
is basically a Vector2
(with a 0 z-axis value). You need to set the mousePosition.z
value to something for it to be placed in a viewable position. Example:
ScreenToWorldPoint采用Vector3,mousePosition基本上是Vector2(具有0 z轴值)。您需要将mousePosition.z值设置为某个值才能将其置于可查看的位置。例:
Vector3 mousePos = Input.mousePosition;
mousePos = cam.ScreenToWorldPoint(mousePos);
mousePos.z = 10;
GameObject.Find("Car1").gameObject.transform.position = mousePos;
This would set the position to 10 units away from the camera.
这样可以将位置设置为距离相机10个单位。
#1
0
Instead of playing with ScreenToWorldPoint
, you should use a raycast against a plane, see this answer: https://*.com/a/29754194/785171.
您应该使用针对飞机的光线投射,而不是使用ScreenToWorldPoint,请参阅以下答案:https://*.com/a/29754194/785171。
The plane should be attached to your AR marker.
飞机应该连接到您的AR标记。
#2
0
ScreenToWorldPoint
takes a Vector3
, mousePosition
is basically a Vector2
(with a 0 z-axis value). You need to set the mousePosition.z
value to something for it to be placed in a viewable position. Example:
ScreenToWorldPoint采用Vector3,mousePosition基本上是Vector2(具有0 z轴值)。您需要将mousePosition.z值设置为某个值才能将其置于可查看的位置。例:
Vector3 mousePos = Input.mousePosition;
mousePos = cam.ScreenToWorldPoint(mousePos);
mousePos.z = 10;
GameObject.Find("Car1").gameObject.transform.position = mousePos;
This would set the position to 10 units away from the camera.
这样可以将位置设置为距离相机10个单位。