1

We're creating a game for graduate college. I'm making a flight shooting game in unity on VR.

The problem is that reset player (aircraft) position to current player's position in real.

Currently, my player object's structure is...

Player
    > XR Origin
        > Camera Offset
            > VR Camera
            > controllers.. etc
    > Aircraft Model

Hierarchy Capture Image

I tried to reset position.

But many tutorials are not flight game, and most of tutorials are re-centered the prepared transform's position.

I want to recenter to current player's real position (in vr).

I know that is possible to recenter in the vr's built in function. But I want to recenter in unity runtime.

Any one has solution? Or is there any method one can call at runtime to recenter the position using vr's built in function?

I using Unity 2022.2.19f, OpenXR package, Oculus Quest 2 (I do not directly use Oculus SDK like OVR...)

Sorry for my poor English skill.. because I'm korean.. :<

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
MayB
  • 11
  • 3

1 Answers1

0

MainCamera --> head

XR Origin --> origin

Desired object for center position --> target

public class ReCenterXROrigin : MonoBehaviour
{
   public Transform head;
   public Transform origin;
   public Transform target;

   private Vector3 offset;
   private Vector3 targetForward;
   private Vector3 cameraForward;
   private float angle;


   private void Start()
   {
      Recenter();
   }

   public void Recenter()
   {
      offset = head.position - origin.position;
      offset.y = 0;
      origin.position = target.position + offset;
      
      targetForward = target.forward;
      targetForward.y = 0;
      cameraForward = head.forward;
      cameraForward.y = 0;

      angle = Vector3.SignedAngle(cameraForward, targetForward, Vector3.up);
      
      origin.RotateAround(head.position, Vector3.up, angle);
   }
}