Unity3D Placing a cube in front VR player and move with player position and rotation

感情迁移 提交于 2020-12-07 05:14:51

问题


I am developing a VR scene using Oculus Quest. I need to place a cube in front and a few units below the user eye level. The cube has to move when the user moves. E.g. if the user turns or moves the cube user secure its position by always staying in front. How can I do this?

Should I take the OVRCameraRig position and rotation and apply that to the cube? But I read from StackOverflow question Unity3D Getting position of OVRCameraRig that OVRCameraRig doesn't move and needs to consider the CenterEyeAnchor details.

Can I extract CenterEyeAnchor rotation and position as below?

OVRCameraRig overCameraRig;

var position = overCameraRig.centerEyeAnchor.position;
var rotation = overCameraRig.centerEyeAnchor.rotation;

//OR should I use lines below?

var position = overCameraRig.centerEyeAnchor.transform.position;
var rotation = overCameraRig.centerEyeAnchor.transform.rotation;

How can I apply the extracted potion and rotation of CenterEyeAnchor to the cube gameObject if that is going to solve this problem?

I tried the following script and add it to the cube but the cube didn't move with the user movement.

Further, I see "Test position" log but no "Normal " or "Transform " logs.

public class MoveCube : MonoBehaviour
{
private OVRCameraRig overCameraRig;
public float offset = 10;
private GameObject itemObject;
private float distance = 3.0f;

// Update is called once per frame
void Update()
{
    Debug.Log("Test position");
    Debug.Log("Normal " +overCameraRig.centerEyeAnchor.position);
    Debug.Log("Transform " + overCameraRig.centerEyeAnchor.transform.position);

    if (itemObject != null)
    {
        itemObject.transform.position = overCameraRig.centerEyeAnchor.transform.position + overCameraRig.centerEyeAnchor.transform.forward * distance;
        itemObject.transform.rotation = new Quaternion(0.0f, overCameraRig.centerEyeAnchor.transform.rotation.y, 0.0f, overCameraRig.centerEyeAnchor.transform.rotation.w);
    }
   
}

}


回答1:


The best thing to do is set the cube as a child of the player in the hierarchy.

only will that not rotate with the player, but i suspect that your camera is on your player so do this:

but this will rotate on all axes. you can also freeze its rotation using the rigidbody freeze rotation.



来源:https://stackoverflow.com/questions/63831411/unity3d-placing-a-cube-in-front-vr-player-and-move-with-player-position-and-rota

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