Unity中如何让物体和相机一起动

Unity中开发VR或者AR应用中我们想要物体和相机跟随着进行移动,我们需要先获得相机的参数,其次我们需要修改物体的参数使得其跟随移动  


public class TestCubeStability : MonoBehaviour
{
    public GameObject mainCamera;
    // Start is called before the first frame update
    void Start()
    {
        mainCamera = GameObject.Find("Main Camera");
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 Cam_Pos = mainCamera.transform.position;
        Vector3 Cam_forward = mainCamera.transform.forward;
        Vector3 Cam_right = mainCamera.transform.right;
        Vector3 Cam_up = mainCamera.transform.up.normalized;
        Vector3 OffSet = new Vector3(0, 1, 0);

        Debug.Log(Cam_Pos);

        this.transform.rotation = mainCamera.transform.rotation;
        this.transform.position = Cam_Pos + Cam_forward;
    }
}

使用this指针可以显示的改变本脚本下物体的参数,别忘了将该脚本挂载到物体下,就可以自动执行了

猜你喜欢

转载自blog.csdn.net/Aaron9489/article/details/128117292