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

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

问题描述

我想弄清楚如何修改示例 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?

推荐答案

要将对象放置在相机视图的中间,您必须更改目标游戏对象的 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天全站免登陆