Placing objects right in front of camera

我只是一个虾纸丫 提交于 2021-01-28 08:11:21

问题


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?

I have tried lots, and although I am somewhat a beginner, I have come up with this

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?


回答1:


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;

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;



回答2:


Arman's suggestion works. Also giving credit to AlmightyR since they got me started in the right direction. Here's what I have now:

// Set a position in front of the camera
float distance = 1;
Vector3 cameraPoint = m_firstPersonCamera.transform.position + m_firstPersonCamera.transform.forward * distance;

// Intanstiate an Andy Android object as a child of the anchor; it's transform will now benefit
// from the anchor's tracking.
var andyObject = Instantiate(m_andyAndroidPrefab, cameraPoint, Quaternion.identity,anchor.transform);

The only problem with this is that because of the existing HelloAR example code, an object is only placed if you click on a point in the point cloud in my case (or a point on a plane by default). I would like it to behave so that you click anywhere on screen, and it places an object anchored to a nearby point in the point cloud, not necessarily one that you clicked. Any thoughts for how to do that?

Side tip for those who don't know: If you want to place something anchored to a point in the cloud, instead of on a plane, change

TrackableHitFlag raycastFilter = TrackableHitFlag.PlaneWithinBounds | TrackableHitFlag.PlaneWithinPolygon;

to

TrackableHitFlag raycastFilter = TrackableHitFlag.PointCloud;


来源:https://stackoverflow.com/questions/46858950/placing-objects-right-in-front-of-camera

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!