Search Unity

Question Move AR Camera Object - X and Z Plane Only

Discussion in 'AR' started by Cornysam, Dec 14, 2020.

  1. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,464
    Hello,

    I am working on an AR mobile app and am looking to move the camera gameobject forward in the direction the camera is facing, but only on the X and Z axis, not the Y. Currently, I can move it on all 3, which is nice, but i don't want the users to be able to "fly".

    Here is the code to fly on all 3 axis:
    Code (CSharp):
    1. arCamera.transform.position = arCamera.transform.position + Camera.main.transform.forward * moveSpeed * Time.deltaTime;
    Here is the code i have messed with to try and get it to work only on the X and Z:
    Code (CSharp):
    1. arCamera.transform.position =
    2.                 new Vector3(arCamera.transform.position.x * Camera.main.transform.forward.x + (Time.deltaTime * moveSpeed),
    3.                 0f,
    4.                 arCamera.transform.position.z * Camera.main.transform.forward.z + (Time.deltaTime * moveSpeed));
    The 2nd code clip kind of works, and by that i mean it doesnt move on the Y axis like i want, but if i am not looking straight forward, then the move forward will actually move the camera backwards or barely at all. So it works in sense by only working on X and Z, but doesnt work as intended with moving in the direction of the camera perfectly.

    Any help is appreciated.