Skip to main content
Up and Coming
June 21, 2015

How do I move the Camera?

Hello, I had previously created a Unity animation and there is a C# script attached to my "Main Camera" that allows the arrow keys to move the camera around. I upgraded to Unity 5.1 to try using my project on the Rift, and the project launched fine, and the head-tracking allows me to look around the scene, but my keys to move the camera no longer work.

Does anyone know what my issue is? It would be helpful if there was a simple Unity sample project for the Rift that shows some simple objects and lets you move around them with the keyboard. I'm new to both the Rift and Unity so some "Hello World" type projects to download would be great. I see some related YouTube videos, but these are mostly for using older versions of Unity.

Thanks!
    This topic has been closed for replies.

    19 replies

    Inspiring
    June 21, 2015
    Not sure why it wouldn't work. Most of the basic code from Unity 4 still works in Unity 5.

    However, here is a script you can toss on your camera that should work fine.

    using UnityEngine;
    using System.Collections;

    public class FlyControl : MonoBehaviour
    {
    public float speed = 10;

    void Start()
    {

    }

    void Update()
    {
    if (Input.GetKey(KeyCode.A))
    {
    Strafe(speed * Time.deltaTime);
    }
    if (Input.GetKey(KeyCode.D))
    {
    Strafe(-speed * Time.deltaTime);
    }
    if (Input.GetKey(KeyCode.W))
    {
    Fly(speed * Time.deltaTime);
    }
    if (Input.GetKey(KeyCode.S))
    {
    Fly(-speed * Time.deltaTime);
    }

    float dx = Input.GetAxis("Mouse X");
    float dy = Input.GetAxis("Mouse Y");
    Look(new Vector3(dx, dy, 0.0f) * 1.25f);
    }

    void Strafe(float dist)
    {
    transform.Translate(Vector3.left * dist);
    }

    void Fly(float dist)
    {
    transform.Translate(Vector3.forward * dist);
    }

    void Look(Vector3 dist)
    {
    Vector3 angles = transform.eulerAngles;
    angles += new Vector3(-dist.y, dist.x, dist.z);
    transform.eulerAngles = new Vector3(ClampAngle(angles.x), angles.y, angles.z);
    }

    float ClampAngle(float angle)
    {
    if (angle < 180f)
    {
    if (angle > 80f) angle = 80f;
    }
    else
    {
    if (angle < 280f) angle = 280f;
    }
    return angle;
    }
    }


    EDIT: No this won't work right with VR. See my post below.
    AMD Ryzen 7 1800X | MSI X370 Titanium | G.Skill 16GB DDR4 3200 | EVGA SuperNOVA 1000 | Corsair Hydro H110i Gigabyte RX Vega 64 x2 | Samsung 960 Evo M.2 500GB | Seagate FireCuda SSHD 2TB | Phanteks ENTHOO EVOLV
    gowgowAuthor
    Up and Coming
    June 21, 2015
    Thanks! Your script works fine, but only when I don't run the project in the Rift (Virtual Reality Supported = unchecked).
    It's strange.

    Also, is the "Unity 4 Integration" still needed at all"? I imported that and tried using an OVRCameraRig instead of a Regular camera. And it seemed to work in the Unity window (both head tracking and the movement), but the Rift wasn't turning on.
    gowgowAuthor
    Up and Coming
    June 21, 2015
    In Rift mode, I can assign the script to an object and use it to fly it around the scene, but I still haven't managed to get it to control the camera. It seems like a bug.
    Up and Coming
    June 21, 2015
    As far as I can tell, in VR mode Unity owns the Main Cameras transform. This is so it can update local rotation and local position based on the HMD.

    To move it around, you need to move/rotate a "parent" game object of the camera. Just add an empty game object, move the camera below it, and add your scripts to the parent.

    HTH

    Peter
    Check out my Unity VR development blog: http://talesfromtherift.com @peterept
    gowgowAuthor
    Up and Coming
    June 21, 2015
    Thanks Peter, this was exactly the problem, and your suggestion resolved it.
    New Participant
    June 21, 2015
    Hi guys,
    I have the exact same issue, but when I try to add the script to an empty game object I get this message :
    https://dl.dropboxusercontent.com/u/368267/Unity_Error.jpg

    Any ideas?
    Cheers
    Stan
    Inspiring
    June 21, 2015
    If you copied my script, you have to name the class the same as the file name:

    public class FlyControl : MonoBehaviour


    So "FlyControl" should be "Move_CamVR" in your case. Hope that helps.
    AMD Ryzen 7 1800X | MSI X370 Titanium | G.Skill 16GB DDR4 3200 | EVGA SuperNOVA 1000 | Corsair Hydro H110i Gigabyte RX Vega 64 x2 | Samsung 960 Evo M.2 500GB | Seagate FireCuda SSHD 2TB | Phanteks ENTHOO EVOLV
    New Participant
    June 21, 2015
    You rock! :D

    Thanks!
    gowgowAuthor
    Up and Coming
    June 22, 2015
    cybereality, you're script is working well now, but I'm wondering if you might know how to modify it so that the camera flies in the direction the user is looking? In my program the user is flying high in the sky, and the current script is a bit awkward in that I don't always travel in the direction I want to. Thanks!
    Inspiring
    June 23, 2015
    OK, I figured it out. Put this script on an empty game object:

    using UnityEngine;
    using System.Collections;
    using UnityEngine.VR;

    public class FlyControl : MonoBehaviour
    {
    public float keySpeed = 10.0f;
    public float mouseSpeed = 1.25f;
    public GameObject eye;

    void Update()
    {
    if (Input.GetKey(KeyCode.A))
    {
    Strafe(-keySpeed * Time.deltaTime);
    }
    if (Input.GetKey(KeyCode.D))
    {
    Strafe(keySpeed * Time.deltaTime);
    }
    if (Input.GetKey(KeyCode.W))
    {
    Fly(keySpeed * Time.deltaTime);
    }
    if (Input.GetKey(KeyCode.S))
    {
    Fly(-keySpeed * Time.deltaTime);
    }

    float dx = Input.GetAxis("Mouse X");
    float dy = Input.GetAxis("Mouse Y");
    Look(new Vector3(dx, dy, 0.0f) * mouseSpeed);
    }

    void Strafe(float dist)
    {
    transform.position += eye.transform.right * dist;
    }

    void Fly(float dist)
    {
    transform.position += eye.transform.forward * dist;
    }

    void Look(Vector3 dist)
    {
    Vector3 angles = transform.eulerAngles;
    angles += new Vector3(-dist.y, dist.x, dist.z);
    transform.eulerAngles = new Vector3(ClampAngle(angles.x), angles.y, angles.z);
    }

    float ClampAngle(float angle)
    {
    if (angle < 180f)
    {
    if (angle > 80f) angle = 80f;
    }
    else
    {
    if (angle < 280f) angle = 280f;
    }
    return angle;
    }
    }


    Then put a standard camera (like Main Camera) as the only child of that object. Also be sure to set the "eye" object to the camera. Hope that works for you.
    AMD Ryzen 7 1800X | MSI X370 Titanium | G.Skill 16GB DDR4 3200 | EVGA SuperNOVA 1000 | Corsair Hydro H110i Gigabyte RX Vega 64 x2 | Samsung 960 Evo M.2 500GB | Seagate FireCuda SSHD 2TB | Phanteks ENTHOO EVOLV