Search Unity

Rotation of headset causes models to jump

Discussion in 'VR' started by JD_FVTC, Apr 22, 2020.

  1. JD_FVTC

    JD_FVTC

    Joined:
    Oct 20, 2017
    Posts:
    54
    In my quest app my model jumps when I rotate my head towards it at a very specific point.
    The handle bar 3D model is moved based on the controllers which are mounted on an actual handle bar in real life.

    This is where it should be positioned.
    Capture.PNG
    This is where it jumps to once I look down and to my left.
    Capture2.PNG
    Its slants backward.
    Capture3.PNG

    The scripts I'm using to calculate the position of the handle bars is below. That works but gets buggy when I look around my world.

    Any Ideas?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RotateWith : MonoBehaviour
    6. {
    7.     public OVRInput.Controller controller = OVRInput.Controller.None;
    8.  
    9.     public Transform centerEye;
    10.  
    11.     public Vector3 rotationAxis = Vector3.forward;
    12.     public Vector3 comparisonVector = Vector3.one;
    13.  
    14.     private Quaternion? _startingRotation;
    15.  
    16.     private Pose? _startingPose;
    17.     private Quaternion _targetLocalRotaiton;
    18.  
    19.     public float RotationSpeed = 1f;
    20.  
    21.     private void Start()
    22.     {
    23.         _targetLocalRotaiton = transform.localRotation;
    24.     }
    25.  
    26.     private Quaternion? GetControllerRotation()
    27.     {
    28.         if (OVRInput.GetControllerOrientationValid(this.controller))
    29.         {
    30.             var localRotation = OVRInput.GetLocalControllerRotation(this.controller);
    31.  
    32.             Quaternion worldRotation = centerEye.rotation * localRotation;
    33.  
    34.             return localRotation;
    35.         }
    36.  
    37.         return null;
    38.     }
    39.  
    40.  
    41.     private void Update()
    42.     {
    43.  
    44.         if (OVRInput.GetControllerOrientationValid(this.controller))
    45.         {
    46.  
    47.             if (_startingRotation.HasValue)
    48.             {
    49.  
    50.                 var controllerLocalRotation = GetControllerRotation();
    51.  
    52.                 if (controllerLocalRotation.HasValue)
    53.                 {
    54.                     // we have a starting rotation, and current rotation,
    55.                     // find the angles between the two (around the rotation axis)
    56.  
    57.                     Vector3 worldRotationAxis = this.transform.TransformVector(this.rotationAxis);
    58.  
    59.                     Vector3 controllerLocalRotationAxis = this.centerEye.InverseTransformVector(worldRotationAxis);
    60.  
    61.                     Vector3 v1 = _startingRotation.Value * comparisonVector;
    62.                     Vector3 v2 = controllerLocalRotation.Value * comparisonVector;
    63.  
    64.                     float angles = Vector3.SignedAngle(v1, v2, controllerLocalRotationAxis);
    65.  
    66.                     _targetLocalRotaiton = Quaternion.AngleAxis(angles, rotationAxis);
    67.                 }
    68.             }
    69.             else
    70.             {
    71.                 _startingRotation = GetControllerRotation();
    72.             }
    73.  
    74.             transform.localRotation = _targetLocalRotaiton;
    75.         }
    76.     }
    77. }
    78.