2
votes

I'm working on a Google Cardboard project, right now i have a demo for Android where u can look around in a special scene i build in UNITY 3D, everything is working fine & looking good, but what I really want is:

I want to walk forward when I press the Google Cardboard magnet button.

I found a few script's on the web, but I do not know exactly how to make these scripts work in my UNITY project.

Can anybody help me further with this?

1
User CaseyB on GitHub posted a utility for this that uses Unity's event system- github.com/CaseyB/UnityCardboardTrigger/tree/develop With this script on an object, the event OnCardboardTrigger() will run on other scrips attached to the same object when the button is pressed - Agumander
Hey Agumander, thank you for you'r response! :) If i put the script on my FirstPersonController, what should i do then to make the controller move? - Nico abbink
Put a function in your script called OnCardboardTrigger(), and inside that function you would put your movement control. As far as I can tell, this function would fire for every frame you have the button down so that movement control could be as simple as transform.position += transform.forward * Time.deltaTime * speed; - Agumander
Hey Agumander! I tried to do what u said, but i think it's still not working. My mobile doesn't give any sign of seeing the magnet.. even when i try Application.quit(); I putted the script from GitHub on my firstpersoncontroller, and added a script called: Button_press. Maybe i'm doing something extremely rong, i hope u could tell me then, gehehe.. [link] (nicoabbink.nl/screens/1.png) [/link] link [/link] - Nico abbink
hi.. i have done set a single image in google cardboard vr surface.. and zoom in and zoom out process also.. i am working on head movement but not getting success.. if you have any idea then assist me. - Amardeepvijay

1 Answers

2
votes

Assuming you are able to read the magnet input correctly. This is how I did an FPS style controller script:

  1. In Unity5 import the asset package Standard Assets/Characters.
  2. Create an instance of RigidBodyFPSController.prefab from that package.
  3. Remove it's child object, "MainCamera"
  4. Import the Google cardboard unitypackage.
  5. Replace the "MainCamera" you removed in step #3 with CardboardMain.prefab
  6. Update or modify a copy of RigidbodyFirstPersonController.cs GetInput() method.

GetInput() with Google Cardboard forward movement fallback:

private Vector2 GetInput()
{
    Vector2 input = new Vector2
    {
        x = Input.GetAxis("Horizontal"),
        y = Input.GetAxis("Vertical")
    };

    // If GetAxis are empty, try alternate input methods.
    if (Math.Abs(input.x) + Math.Abs(input.y) < 2 * float.Epsilon)
    {
        if (IsMoving) //IsMoving is the flag for forward movement. This is the bool that would be toggled by a click of the Google cardboard magnet
        {
            input = new Vector2(0, 1); // go straight forward by setting positive Vertical
        }
    }
    movementSettings.UpdateDesiredTargetSpeed(input);
    return input;
}

Google's SDK only support's detecting a magnet "click". If you want to hold down the magnet to move forward, I recommend using Cardboard Controls+ from the Unity3D Asset Store.