1
votes

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
What happened when you tried this?Ruzihm

1 Answers

1
votes

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

Like this

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

This will rotate with the camera

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