How to convert a position into starting point for

2019-08-18 10:37发布

Trying to develop an AR app.When the app is opened,at that point the device location is (0,0,0) that is if I print or display my coordinates it will be (0,0,0).I want to create a starting point like at the entrance of a door.When other users using my app can open the app anywhere.

What I am trying to do is I already kept an AR at the entrance of the door.Users open app at random position which become their starting point, all AR objects will appear.When they pass through the AR object ,I want the coordinates of their device be(0,0,0).But if I run the below code in unity editor it takes the camera to the position where app has started.I am looking to convert the entrance position into App starting point.

            Camera.main.transform.position=new Vector3(0,0,0);

From what I understand if we change the position of camera in app it can show glitches

标签: unity3d arkit
1条回答
不美不萌又怎样
2楼-- · 2019-08-18 10:55

Your question is very unclear -.-

but IF what you try doing is reset the camera to (0,0,0) while keeping all game objects at the same relative position to it you could try:

    var localToCamera = Camera.main.transform.worldToLocalMatrix;
    var obj = GameObject.FindSceneObjectsOfType(typeof(GameObject));
    foreach (var o in obj)
    {
        var go = (o as GameObject);
        go.transform.FromMatrix(localToCamera * go.transform.localToWorldMatrix);
    }

_

EDIT:

what the UGLY code above is supposed to do: it's going to crawl through all GameObjects and reposition them in such a way that if you set the camera to position (0,0,0) and facing (0,0,1), they will remain at the same position and orientation relatively to the camera.

notice that the camera itself WILL get repositioned to (0,0,0) and facing (0,0,1) after this code is executed because

Camera.main.transform.worldToLocalMatrix * localToWorldMatrix == identity

EDIT:

I can't put this code in the comments because it's too long.

try

// code from the top of my head here, syntax or function names might not be exact
var forward = arcamera.transform.forward;
forward.y = 0;
arcamera.transform.rotation = Quaternion.LookDirection(forward);
// now the camera forward should be in the (x,z) plane
UnityARSessionNativeInterface.GetARSessionNativeInterface().SetWorldOrigin(arcamera.transform);
// since you did not tilt the horizontal plane, hopefully the plane detection still detected vertical and horizontal planes
查看更多
登录 后发表回答