Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to make the OVR camera fly in Unity?

Discussion in 'AR/VR (XR) Discussion' started by Frank Chai, Jan 27, 2016.

  1. Frank Chai

    Frank Chai

    Joined:
    Jan 27, 2016
    Posts:
    2
    I want to create a flying experience in Unity, by using Oculus and Xbox controller to fly. Just like some RPG games, use Oculus to control turn around and use Xbox controller to move. Can anyone here help me to solve this problem? Thanks!
     
  2. JesseLBusch

    JesseLBusch

    Joined:
    Aug 6, 2014
    Posts:
    4
    If I understand your question correctly, your simply trying to apply movement to the camera which gets somewhat locked because of the nature of Unity's VR implementation.

    You can move the camera by making it a child under another game object. The camera will maintain the headset's orientation/position in local space underneath the parent object. At least, that's how we're doing it.
     
  3. Frank Chai

    Frank Chai

    Joined:
    Jan 27, 2016
    Posts:
    2
    OK I totally understand what you said, but how to make the new object fly? Do you have some tutorial links? or some open source code? Thank you very much!
     
  4. JesseLBusch

    JesseLBusch

    Joined:
    Aug 6, 2014
    Posts:
    4
  5. reyn-mukai

    reyn-mukai

    Joined:
    Jun 19, 2017
    Posts:
    1
    In case anyone else needs help creating a fly camera in Unity using the OVRPlayerController as a base, I wrote a quick code for an Xbox One controller. This script can easily be modified to use keyboard inputs.

    To use this script, attach it to the base OVRPlayerController in the hierarchy and disable all other scripts. Add a Rigidbody component, freeze all rotations, and disable gravity. Finally, attach the CenterEyeAnchor in the hierarchy to the Center Eye Anchor field in the inspector.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FlyCamera : MonoBehaviour {
    5.  
    6.     public static float standardSpeed = 3.0f;
    7.     public static float fastSpeed = 15.0f;
    8.     public static float rotationSpeed = 60.0f;
    9.     public static float orientation = 0.0f;
    10.     public static float positionalSpeed = 7.5f;
    11.  
    12.     float speed = standardSpeed;
    13.  
    14.     public GameObject CenterEyeAnchor;
    15.  
    16.     Rigidbody rb;
    17.  
    18.     void Start (){
    19.         rb = GetComponent<Rigidbody> ();
    20.      }
    21.  
    22.     void Update () {
    23.         Vector2 primaryAxis = OVRInput.Get(OVRInput.Axis2D.PrimaryThumbstick);
    24.         Vector2 secondaryAxis = OVRInput.Get(OVRInput.Axis2D.SecondaryThumbstick);
    25.         float primaryIndex = OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger);
    26.         float secondaryIndex = OVRInput.Get(OVRInput.Axis1D.SecondaryIndexTrigger);
    27.  
    28.         if(OVRInput.Get(OVRInput.Button.PrimaryThumbstick) || OVRInput.Get(OVRInput.Button.DpadUp) || OVRInput.Get(OVRInput.Button.DpadDown) || OVRInput.Get(OVRInput.Button.DpadLeft) || OVRInput.Get(OVRInput.Button.DpadRight)) {
    29.             speed = fastSpeed;
    30.         }
    31.         else {
    32.             speed = standardSpeed;
    33.         }
    34.  
    35.         // Dpad Movement
    36.         if (OVRInput.Get(OVRInput.Button.DpadUp) || Input.GetKeyDown(KeyCode.W)){
    37.           rb.velocity = CenterEyeAnchor.transform.forward * speed;
    38.         }
    39.         else if (OVRInput.GetUp(OVRInput.Button.DpadUp) && (primaryAxis.y == 0.0f && secondaryAxis.x == 0.0f)){
    40.           rb.velocity = CenterEyeAnchor.transform.forward * 0.0f;
    41.         }
    42.         if (OVRInput.Get(OVRInput.Button.DpadDown) || Input.GetKeyDown(KeyCode.S)){
    43.           rb.velocity = CenterEyeAnchor.transform.forward * speed * -1;
    44.         }
    45.         else if (OVRInput.GetUp(OVRInput.Button.DpadDown) && (primaryAxis.y == 0.0f && secondaryAxis.x == 0.0f)){
    46.           rb.velocity = CenterEyeAnchor.transform.forward * 0.0f;
    47.         }
    48.         if (OVRInput.Get(OVRInput.Button.DpadRight)){
    49.           rb.velocity = CenterEyeAnchor.transform.right * speed;
    50.         }
    51.         else if (OVRInput.GetUp(OVRInput.Button.DpadRight) && (primaryAxis.y == 0.0f && secondaryAxis.x == 0.0f)){
    52.           rb.velocity = CenterEyeAnchor.transform.right * 0.0f;
    53.         }
    54.         if (OVRInput.Get(OVRInput.Button.DpadLeft)){
    55.           rb.velocity = CenterEyeAnchor.transform.right * speed * -1;
    56.         }
    57.         else if (OVRInput.GetUp(OVRInput.Button.DpadLeft) && (primaryAxis.y == 0.0f && secondaryAxis.x == 0.0f)){
    58.           rb.velocity = CenterEyeAnchor.transform.right * 0.0f;
    59.         }
    60.  
    61.         // Left Analog Stick Movement (Camera Face Movement)
    62.         if (primaryAxis.x != 0.0f || primaryAxis.y != 0.0f){
    63.           rb.velocity = CenterEyeAnchor.transform.forward * speed * primaryAxis.y + CenterEyeAnchor.transform.right * speed * primaryAxis.x;
    64.         }
    65.         else if (primaryAxis.x == 0.0f && primaryAxis.y == 0.0f && (OVRInput.Get(OVRInput.Button.DpadUp) == false && OVRInput.Get(OVRInput.Button.DpadDown) == false && OVRInput.Get(OVRInput.Button.DpadRight) == false && OVRInput.Get(OVRInput.Button.DpadLeft) == false) && rb.velocity.magnitude > 0){
    66.           rb.velocity = CenterEyeAnchor.transform.forward * 0.0f + CenterEyeAnchor.transform.right * 0.0f;
    67.         }
    68.  
    69.         // Right Analog Stick Player Rotation (This can cause player disorientation)
    70.         if (secondaryAxis.x != 0.0f){
    71.           orientation = orientation + rotationSpeed * secondaryAxis.x * Time.deltaTime;
    72.           rb.rotation = Quaternion.Euler(0,orientation,0);
    73.         }
    74.  
    75.         // Triggers Vertical Movement (Game World Vertical Movement)
    76.         if(primaryIndex != 0.0f || secondaryIndex != 0.0f){
    77.           rb.velocity = transform.up * positionalSpeed * (secondaryIndex - primaryIndex);
    78.         }
    79.     }
    80. }
    81.  
     
    SelfishReplicator likes this.
  6. Selzier

    Selzier

    Joined:
    Sep 23, 2014
    Posts:
    652
  7. markncolleen1985

    markncolleen1985

    Joined:
    Nov 21, 2017
    Posts:
    5
    HI, im looking to be able to use the trackpad on the gear vr controller to walk primaryAxis and SecondaryAxis i think its called, i am using the stock ovrplayercontroller, and it only moves with swipes, any help please?
     
  8. danieladominguezcoll

    danieladominguezcoll

    Joined:
    Sep 27, 2018
    Posts:
    5
    Try this one.
     

    Attached Files:

  9. MohIsm

    MohIsm

    Joined:
    Nov 10, 2015
    Posts:
    1
    I know the post is kind of old but I was trying to do the same thing.
    I'm in no means an expert and I'm sure there are better ways to do it.
    I started by making the OVRPlayer able to move in the air. The OVRPlayer has an if statement that stops the character from moving while its in the air if(!Controller.IsGrounded) : and I changed the scale to match the one on top by copying the float which was about 0.7.
    Then changed the gravity to 0 and now we have a camera that can move in the air.
    But the it wouldn't move up where I was looking.
    If you look for "ortEuler.z = ortEuler.x = 0f;" it kept the orientation at 0 so I took ortEuler.x out keeping "ortEuler.z = 0f"
    It still didn't work because x was still being clipped here "transform.rotation = Quaternion.Euler(0.0f, centerEye.rotation.eulerAngles.y, 0.0f);"
    Changed that statement to "transform.rotation = Quaternion.Euler(centerEye.rotation.eulerAngles.x , centerEye.rotation.eulerAngles.y, 0.0f);" and the script started doing what I wanted it to.
    I know my explanation is pretty long and boring but I'm not sure if the reader is just beginning scripting or has done it in a while, in the hopes that it can help someone.

    Summary
    1) Change (!Controller.IsGrounded) to desired float (I used 0.7f)
    2) Delete ortEuler.x = 0f but keep z
    3) add centerEye.rotation.eulerAngles.x into transform.rotation
    4) Make sure gravity is 0 to make it fly
     
    edjgarciabo and mkpaula8g like this.
  10. NoM16

    NoM16

    Joined:
    May 29, 2019
    Posts:
    2
     
  11. mfarhanrosli21

    mfarhanrosli21

    Joined:
    Sep 18, 2019
    Posts:
    2
    Does this works on oculus rift?
     
  12. dwatt-hollowworldgames

    dwatt-hollowworldgames

    Joined:
    Apr 26, 2019
    Posts:
    104
    Child the OVRCameraRig to a gameobject and move the gameobject to your hearts content.