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

    Participant
    June 23, 2015
    This works for me as well, thanks for the post.
    gowgowAuthor
    Up and Coming
    June 23, 2015
    Hey thanks cybereality! Your new scrips is a lot closer to what I was looking for. For the most part the "w" key seems to fly in the direction I'm looking. Some of the time this doesn't seem to work quite right(like maybe after I've done some strafing?) but it's a lot closer to what I was wanting.
    gowgowAuthor
    Up and Coming
    June 26, 2015
    I wasn't using this quite right. For anyone trying to use this. Here's what worked for me.

    I attached the FlyControl script to the parent object of the camera. Then, within the script, I added the following to initialize the "eye" object to be the camera game object.

    void Awake () {
    Debug.Log("In Away for Fly Control");
    eye = GameObject.FindWithTag ("Camera");
    }


    It's working now.


    great now.
    Inspiring
    July 9, 2015
    Here's an updated script based on Quaternions so it avoids gimbal lock. Even so, you should avoid looking too far up or down as only mouse movement is clamped (head-tracking can go beyond and thus flip the movement). You will want to put this on an empty game object with the camera as a child. Then set eye to the camera in the inspector.

    using UnityEngine;
    using System.Collections;

    public class FreeLook : MonoBehaviour
    {
    public float keySpeed = 10;
    public float mouseSpeed = 1.25f;
    public GameObject eye;

    private Quaternion originalRotation;
    private Vector2 angle = new Vector2(0f, 0f);
    private Vector2 minAngle = new Vector2(-360f, -90f);
    private Vector2 maxAngle = new Vector2(360f, 90f);
    private float limit = 360f;

    // Use this for initialization
    void Start()
    {
    Cursor.lockState = CursorLockMode.Locked;
    Cursor.visible = false;
    originalRotation = transform.localRotation;
    }

    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 Vector2(dx, dy) * mouseSpeed);
    }

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

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

    void Look(Vector2 dist)
    {
    angle += dist;

    angle.x = ClampAngle(angle.x, minAngle.x, maxAngle.x);
    angle.y = ClampAngle(angle.y, minAngle.y, maxAngle.y);

    Quaternion quatX = Quaternion.AngleAxis(angle.x, Vector3.up);
    Quaternion quatY = Quaternion.AngleAxis(angle.y, -Vector3.right);

    transform.localRotation = originalRotation * quatX * quatY;
    }

    float ClampAngle(float angle, float min, float max)
    {
    if (angle < -limit)
    {
    angle += limit;
    }
    else if (angle > limit)
    {
    angle -= limit;
    }
    return Mathf.Clamp(angle, min, max);
    }
    }
    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
    July 12, 2015
    Well done all, this is very helpful and solved my problems immediately. Thanks!
    New Participant
    August 30, 2015
    "peterept" wrote:
    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


    Excellent solution, worked perfectly, thanks :D
    New Participant
    July 18, 2017
    I'm having issues with this script...
    New Participant
    July 25, 2017
    you must replace the HTML-Entities with their correct equivalent (you have copied the script so they can occur).

    &lt; =   <
    &gt; =   >

    Best,
    Florian
    My website: www.equbytes.com
    New Participant
    August 1, 2017

    equbytes said:

    you must replace the HTML-Entities with their correct equivalent (you have copied the script so they can occur).

    &lt; =   <
    &gt; =   >

    Best,
    Florian


    I don't even remember posting on this thread, but thank god I did because this fixed my issue! Thank you!