将物体放置在相机的正前方 [英] Placing objects right in front of camera

查看:117
本文介绍了将物体放置在相机的正前方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从示例ARCore场景中找出如何修改HelloARController.cs,以将对象直接放置在相机前面.我的想法是,我们正在将光线从摄像机射线投射到锚点或被跟踪平面上的Vector3,所以我们不能获取该光线起点的Vector3并在该点处或附近放置一个对象吗?

I am trying to figure out how to modify HelloARController.cs from the example ARCore scene to place objects directly in front of the camera. My thinking is that we are raycasting from the camera to a Vector3 on an anchor or tracked plane, so can't we get the Vector3 of the start of that ray and place an object at or near that point?

我已经尝试了很多,尽管我是一个初学者,但我想出了这个

据我了解,ScreenToWorldPoint应该输出与世界相对应的屏幕位置的vector3,但是它无法正常工作.我尝试了其他选项,而不是ScreenToWorldPoint,但是没有任何效果令人满意.有人有提示吗?

From my understanding, ScreenToWorldPoint should output a vector3 of the screen position corresponding to the world, but it is not working correctly. I have tried other options instead of ScreenToWorldPoint, but nothing has presented the desired effect. Does anyone have any tips?

推荐答案

要将对象放置在摄像机视图的中间,您必须更改目标gameObject的transform.position(如AlmightyR所说).

To place the object right at the middle of the camera's view, you would have to change the target gameObject's transform.position (as AlmightyR has said).

准备好的代码如下所示:

The ready code would look something like this:

GameObject camera;
GameObject object;
float distance = 1;

object.transform.position = camera.transform.position + camera.transform.forward * distance;

由于摄像机的前向分量(Z轴)始终沿摄像机所望的方向倾斜,因此您可以将矢量的方向乘以要放置对象的距离.如果您希望无论照相机如何移动,对象始终保持在该位置,则可以使其成为照相机变换的子代.

Since camera's forward component (Z axis) is always poiting at the direction where Camera is looking to, you take that vector's direction and multiply it by a distance you want your object to be placed on. If you want your object to always stay at that position no matter how camera moves, you can make it a child of camera's transform.

object.transform.SetParent(camera.transform);
object.transform.localPosition = Vector3.forward * distance;

这篇关于将物体放置在相机的正前方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆