将物体放置在相机的正前方

编程入门 行业动态 更新时间:2024-10-18 16:54:12
本文介绍了将物体放置在相机的正前方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图从示例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;

更多推荐

将物体放置在相机的正前方

本文发布于:2023-06-05 23:07:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/531181.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:物体   相机

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!