Forum Discussion
gowgow
10 years agoHonored Guest
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!
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!
19 Replies
Replies have been turned off for this discussion
- cyberealityGrand ChampionNot 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. - gowgowHonored GuestThanks! 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. - gowgowHonored GuestIn 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.
- petereptProtegeAs 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 - gowgowHonored GuestThanks Peter, this was exactly the problem, and your suggestion resolved it.
- sbrusseHonored GuestHi 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 - cyberealityGrand ChampionIf 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. - sbrusseHonored GuestYou rock! :D
Thanks! - gowgowHonored Guestcybereality, 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!
- cyberealityGrand ChampionOK, 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.
Quick Links
- Horizon Developer Support
- Quest User Forums
- Troubleshooting Forum for problems with a game or app
- Quest Support for problems with your device
Other Meta Support
Related Content
- 4 years ago
- 9 months ago
- 2 years ago