Hey guys,
I’m new to Unity and am working on a project for Google Cardboard. Basically I need a script that will make my camera move forward at a certain speed and by moving my head around in VR I can choose where I go throughout the environment. Its okay that you cant stop for my project.
Any help would be much appreciated.
Thanks
Do you mean head tracking? (camera fixed at eye position basically) or do you actually want to tilt your head to move forward for example?
Not head tracking. I want the camera to be moving foward constantly, if I turn my head to the right for example it will continue moving foward but in that direction. Then I could do a 180 to see what’s behind me then it would move towards that. Basically whatever you are look at you are moving towards.
I’m already using Google Cardboard sdk which has a Main camera which works perfectly.
I dont know exactly how cardboard is setup in unity, but I guess you have 2 cameras which are parented to a GameObject. Cardboard will rotate this GameObject, as you rotate your head? So both cameras rotate with it.
So you want to move this GameObject, in the direction it looks. You can make a script like:
using System.Collections;
using UnityEngine;
public class MoveScript : MonoBehaviour
{
public float speed = 10; // moves 10 units every second
// Use this for initialization
private void Start()
{
}
// Update is called once per frame
private void Update()
{
transform.Translate(transform.forward * Time.deltaTime * speed, Space.World);
}
}
and attach it to the parent of both cameras
Thanks for the response!
So the camera you get goes CardboardMain>Head>MainCamera>MainCameraLeft&MainCameraRight
So theres only one parent to the two cameras.
Whats happening when I attach this script is its moving toward what you look at but does not allow you to rotate 180 and look backwards or to the sides. It is just on the trajectory forward.
You will obviously need another script to update the rotation of your camera based on the motion tracking of the device.
When you press play you can check in the editor which gameobject gets the rotation of your head movement. Maybe you need to attach the script to the head instead.
ok, so combine that with the Input.acceleration and we start getting something…
nah, he already uses Cardboard sdk, which handles all the cam rotation and stuff
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class move : MonoBehaviour
{
public float playerSpeed = 10;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
transform.position = transform.position + Camera.main.transform.forward * playerSpeed * Time.deltaTime;
}
}
Attach this to the main camera. Set your ship or body or boat or whatever to the main camera as a child. (I assume you want it to fly a spaceship)