Programming
Article

Building a Google Cardboard VR App in Unity

By Patrick Catanzariti

Unity is currently one of the most popular choices when it comes to building virtual reality applications. Google Cardboard is a great first choice for getting started in the field – spending $20 on a VR headset rather than potentially hundreds is a much safer bet for getting started (and there are plenty of people with smartphones who could potentially enjoy your app with an equally smaller upfront cost!).

I’ve previously covered how to build VR for the web today via JS, along with demos on Visualizing a Twitter Stream in VR and JS and plenty others. For those who prefer Unity development, it’s about time I evened these numbers and wrote some more on VR development in Unity. For some rather strange reason, I couldn’t find many in depth or varied guides to working with the Google Cardboard Unity SDK from scratch, so I did what any SitePoint writer would do. I put one together!

Update as of 21st March: Since writing this article, Google has updated their SDK and changed how the crosshair functionality works. We have a new article covering these changes here. Most of this tutorial is still accurate, it is just the crosshair functionality which has changed. So follow along with this guide, then read the new article to get your crosshair working!

What You’ll Need

To follow along, you’ll need the following bits of software and hardware:

  • Unity – At least v4.5 is recommended. I’m using Unity v5 Pro for this guide. Unity v5 Personal apparently works a-okay too.
  • Windows or Mac – I’ll be using Windows in this tutorial for a change (mainly because most Unity devs seem to be on Windows, so it’s only fair I follow suit for the article!)
  • Cardboard SDK for Unity – That link will provide you with the GitHub repo. I’ll explain this and setting up Java and Android in this article.
  • Java SE SDK – This will be required to install the Android SDK.
  • Android SDK – If you are downloading this for the first time, you only need the “SDK Tools Only” bits of the SDK. I’ll explain this soon.
  • A Google Cardboard style headset
  • An Android device to put inside that headset – it is possible to export this to work with iOS, but I won’t be covering that process as I don’t have an iPhone!
  • Relatively basic knowledge of Unity – I’ll cover a lot of the absolute basics for those new to it all but having a small amount of knowledge will help!

The Code

Want to jump straight into the code? My sample project is up at GitHub for you to do whatever you’d like!

Preparing the Android SDK

In order to create an Android mobile app, we’ll need the Android SDK working on our system. For seasoned Android developers who’ve already got this running, feel free to skip to the next section!

To begin the Android SDK process, you first will need to check you’ve got the Java SE SDK installed on your system. If you aren’t sure, try skipping to the Android SDK install step first. The installation will complain if you don’t have Java!

Which Java?

If your system is Java-free, it can be a bit confusing to know which of the various Java options you’ll need. Head to the Java SE download page and choose the JDK download option:

Download Java

Once you’ve got that downloaded, run through the very typical install process that looks a bit like so:

The Java Install Screen

Android SDK Download and Install

If you are new to Android development, you’ll need to download the Android SDK and install it. You only need the “SDK Tools Only” bit of the SDK which is available at their Other Download Options page. This provides the bare minimum. You could download the whole Android Studio package if you’re looking to do a lot of Android app development in future. If you are sticking with the “SDK Tools Only” option, head to that page and choose the version for your operating system:

Downloading Android SDK

Run that installation file you’ve downloaded. If you weren’t sure if you had Java installed, the installation should confirm this for you:

Java Exists!

Follow the rest of the prompts (they are pretty standard installation prompts) and install the SDK to your system. When it is done, keep the checkbox ticked before you click Finish, that way you can load the SDK Manager:

Android SDK Success

The SDK Manager should appear and look like so:

SDK Manager

It is likely to have a bunch of checkboxes already selected for you. Let’s reduce it to just what we’ll need for now. Underneath the “Tools” folder, you’ll want the following selected:

  • Android SDK Tools – This should be at the top of the list.
  • Android SDK Platform-tools – This will be right after the SDK Tools.
  • Android SDK Build-tools – You should only need the latest revision of this (you can see the revisions under the “Rev.” column)

That should look like so:

SDK Manager Install Options

Then, within the latest Android API folder (whichever is highest, at the time of writing that’s “Android 6.0 (API 23)”), choose the following:

  • SDK Platform – You’ll need this!
  • A system image – You can choose either an ARM or an Intel system image to allow you to emulate that Android system on your computer. If you’re looking to do all testing on your physical Android device, you can skip this.
  • Google APIs – This will let us use any Google APIs with our app. This doesn’t include Google Cardboard – I’ve just included it for convenience, you can likely skip this one too.

That should look like the following:

SDK Manager System Images

Click “Install X packages” in the bottom right corner to begin the installation process. You’ll need to accept the licence before it will let you install.

Once that is installed, you should have everything you need on the Android side of things!

Creating A Cardboard Empowered Unity Project

From the Cardboard SDK Github repo linked above, all you’ll really need to download is the Cardboard SDK For Unity .unitypackage file here.

Once that has downloaded, we’re ready to begin our actual Unity project. Open up Unity and do the obvious first step – create a new project.

Create a new Unity project

Choose 3D and give your project a name. Don’t worry about adding asset packages on this screen, we’ll be adding our custom one in the next step. Select “Create Project” once you’re ready to begin:

Naming the Unity Project and Setting It to 3D

Next, go to Assets > Import Package > Custom Package… and find your Cardboard SDK .unitypackage file you downloaded earlier.

Add custom package

If you are using Unity 5, you won’t need the legacy folders and can untick those:

Untick legacy for Unity 5

When that is successful, your new Cardboard SDK assets should appear in your project inside a “Cardboard” folder:

Cardboard Assets Added

Within the Cardboard SDK folder, you’ll find four subfolders – “Editor”, “Prefabs”, “Resources” and “Scripts”:

Within the Cardboard Folder

Inside the “Prefabs” folder, you’ll find a Prefab called “CardboardMain.prefab”. Drag that into your scene and raise it up by 2 on the Y axis (otherwise the user will feel like they’re either tiny or super short!):

CardboardMain Prefab Folder

Open up the “CardboardMain” prefab and you’ll find the “Main Camera” within it. This is our two stereoscopic style cameras working in unison to display the scene in wonderous VR when looking at it though a Google Cardboard headset. In our “Main Camera” object, we’ll want to add a “Physics Raycaster” component. To do so, click “Add Component” whilst you’ve got the “Main Camera” open. I typically find it by typing in “raycaster” in the window that pops up. We’ll need a Physics Raycaster to be able to interact with elements by looking at them.

Adding a Raycaster

Next, you can add in an icon to show when the user is looking at an object. I used a free icon in the Unity store from the 64 Flat Game Icons pack by Office57. You could create your own sprite for this too if you’d prefer. Get a sprite from somewhere and import it into your project.

Create an empty game object within the “Head” object in “CardboardMain”. I’ve renamed mine as “Target”. Then drag your sprite inside this object:

Dragging sprite into empty game object

That should be all you need to enable plenty of very useful Cardboard view tracking and controls.

Responding To Cardboard Events

Now we have our CardboardMain view created, we’ll need a few things in the scene to interact with. For my demo, I used the Magic Lamp by 3dFoin and SkySphere Volume 1 by the Hedgehog Team from the Unity Store. You can use any objects you’d like, drag the lamp into your scene:

Dragging in Lamp To Scene

When importing the SkySphere, feel free to untick the “Examples” folder to keep things clean in our project:

Importing Skysphere

Place your lamp at position {x: 0, y: 1.5, z: 2} with a rotation of {x: -90, y: 90, z: 0} and scale it by 0.5 on all axes so it’s not too big in comparison to our scene:

Magic Lamp Settings

Then drag in the “Skyball_WithoutCap” from the SkySphere_V1 > Meshes folder. It should be placed at {0,0,0} with an X rotation of -90:

Skyball without cap

In order to interact with the Cardboard SDK on this object, you’ll need some code on that object. Our initial bit of functionality will focus around causing events to occur when we look at the lamp and then look away. Each time the user looks away from the lamp, the sky will change texture:

using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;

public class Lamp : MonoBehaviour {
  public Renderer skybox;
  public Material sky1;
  public Material sky2;
  public Material sky3;
  public Material sky4;
  private int currentSky = 0;

  void Start () {
    
  }

  public void stareAtLamp() {
    switch (currentSky) {
      case 0:
        skybox.material = sky2;
        currentSky++;
        break;
      case 1:
        skybox.material = sky3;
        currentSky++;
        break;
      case 2:
        skybox.material = sky4;
        currentSky++;
        break;
      case 3:
        skybox.material = sky1;
        currentSky = 0;
        break;
    }
  }
}

The code above is pretty straightforward stuff, we set up a skybox variable, four materials for that skybox and an integer to store the number of the current skybox. Then, we’ve got a public method called stareAtLamp() which changes our skybox’s material to the next one in the series of materials, or takes it back to the first material if we’re at the last one.

To add this code to our project, select your lamp and click “Add Component”. Type in “script” and you should find “New Script…”. Click that and call it “Lamp”. Put in our code from above.

Open in Code Editor

When your code is saved, those public variables will be available as options for your script. Drag your skybox object from the Hierarchy of your project into the skybox variable. Find the materials for your SkySphere from the Assets > SkySphere_V1 > SkySphere > Materials > With_Planet folder (or whatever other materials you’d prefer) and drag a few of them into the four sky materials.

Drag Objects Into Script

We will need one more component within our lamp object – a “Event Trigger”. This is what will actually call the stareAtLamp() function we coded earlier. Head to “Add Component” with your lamp object selected and find the “Event Trigger” component. Then once it is in your lamp object, click “Add New Event Type” and choose “PointerExit”:

Adding Pointer Exit Event

From here, you’ll now have an area to add in responses to that “PointerExit” event. Click the “+” icon on the bottom left and then drag in your lamp object to the box that appears underneath “Runtime Only”:

Setting up event trigger

This allows us to access the lamp’s functions as possible responses. Click on the dropdown that says “No function”, find “Lamp” and then choose the stareAtLamp() function.

Setting the stare at lamp function

In order for the event trigger to work, our scene needs an “Event System”. Head back to your scene and create one from UI > Event System.

Adding event system

You can remove the “Standalone Input Module” from the Event System if you’d like. The main important component we do need is the “Gaze Input Module” from the Cardboard SDK. You can add this one by clicking “Add Component” and going to Scripts > GazeInputModule:

Adding gaze input module

Now we define our target sprite as the one that appears when we are using the GazeInputModule and are looking at an object. Drag the “Target” object (or whatever you chose to call it within your CardboardMain object) into the cursor setting for the component:

Drag Target Into Gaze Input

In order to pick up when we are looking at this lamp, we’ll need to add a collider to it. To do so, click “Add Component” and type in “collider” to find “Sphere Collider”. Add that in:

Adding Sphere Collider

Then, if necessary, adjust the position and radius of the sphere collider so that the green sphere around the lamp covers its shape:

Cover lamp

We are almost ready to test out our VR app!

Delete the “MainCamera” object in your scene if you haven’t already instinctively removed it – the “CardboardMain” object is our new camera.

Before we test, add a simple floor to the scene too. Create a “Plane” object from 3D Object > Plane and ensure it is positioned at {x: 0, y: 0, z: 0}. Scale that plane to {x: 2, y: 1, z: 2} so that it extends out to the whole skybox:

Created Plane

Testing Our App

We should have everything in place to be able to try out our VR app and see how well it plays. To do so, click the Play button at the top of Unity and it should get the app running for you. Two important controls to remember are:

  • To simulate looking around your scene, hold down the Alt key while moving your mouse.
  • To simulate tilting your head, hold down the Shift key while moving your mouse.

You should see your scene running nicely showing your lamp. To see it a bit bigger, there’s a tiny icon on the top right. Click that and choose “Maximise”. You can also click “Maximise on Play” to request it does this automatically when you are testing:

Maximizing Screen

You should have your scene running maximised now:

Playing Scene

If you look at the lamp, your icon should appear indicating that you are indeed looking at the lamp:

Target On Lamp

If you look away, the sky should change like magic!

Looking Away

Excellent, that’s our first use of the Cardboard SDK. However, there’s one more function we haven’t taken advantage of on the Cardboard headset – our clicker on the side. Here’s how to engage that.

Using The Clicker

To add click events from the Cardboard headset, we’ll need to add a new event handler. First, we will set up what that event should actually do. For me, the lamp just isn’t quite magical enough. I’m going to include a particle system that is set off any time the viewer clicks the lamp. To do so, open up the lamp once more and click “Add Component”, then type in “particle”. You should find “Particle System”. Add this onto your lamp. You should now see white particles floating out of it:

Fancy particles

You can play around with the Particle System settings to your own preferences and see what you think looks the coolest! I set the color to be more of a yellow, changed the duration to 10 and set them to go in a random direction via the “Shape” section. One setting you’ll need to change so that the particles only appear on click is to change the “Rate” within “Emission” to be 0:

Particle System Settings

I also ended up using a “Gravity Modifier” of 1, which made the particles look a bit cooler as they fell back down over the lamp. Give that a try!

Within the code, we’ll add a new function called rubLamp() that emits a burst of particles from our Particle System:

public void rubLamp() {
  GetComponent<ParticleSystem>().Emit(10); 
}

Then, we go back to our lamp object’s “Event Trigger” component and add one for “Pointer Click”:

Pointer Click

We then click and add our rubLamp() function as the response:

Rub Lamp

Now if we play our scene and click that lamp, we’ll have particles bursting from it. Quite neat!

Particles Bursting

Getting The App Onto An Android Device

Now we definitely want to get it onto our phone so that we can try it within a Google Cardboard headset. In Unity, go to File > Build Settings. In the screen that appears, choose “Android” from the list and then select “Player Settings”:

Build settings

The settings will appear on the right (you might need to move the “Build Settings” window to the left a little to see it all). Under Resolution and Presentation, change “Default Orientation” to “Landscape Left”:

Build Settings Resolution

Then underneath that, open up the “Other Settings” section and update the bundle identifier to include your company name and the app’s name – com.companyName.appName. I used com.SitePoint.GreatestVRProjectInTheWorld:

Build Settings Name

You should now be ready to go! Connect up your Android phone via USB to your computer and click “Build and Run” in the “Build Settings” window:

Build and Run

Choose where to save your apk file and give it a name:

APK Name

Unity should then run through and do everything else for you. It will place the app on your phone and run it. Once the app is on your phone, disconnect it from your computer, put it into your Google Cardboard and enjoy your VR creation!

The VR app running on mobile

Conclusion

Working with Google Cardboard and Unity is surprisingly really straight forward! From this initial demo, you’re now empowered with all the capabilities of Unity, combined with the capabilities of a whole portable VR platform! A little while back I covered calling Web APIs in Unity – the Cardboard SDK integrates well into that whole process, so you could have an IoT enabled VR Unity app if you combine the two techniques!

Feel free to use this code tutorial as the base for any Cardboard VR project you build in future! If you make something with it, please share it in the comments or get in touch with me on Twitter (@thatpatrickguy). I’d love to check it out!

If you’re looking for other links and sample projects to guide you in VR and Unity development, I’ve got a set of curated links that might be perfect for you! Head over to Dev Diner and check out my VR with Unity Developer Guide, full of links to resources around the web. If you’ve got other great resources I don’t have listed – please let me know too!

  • http://www.imobdevtech.com/ Dev Patel

    Really Informative article, thanks for sharing such a valuable information Mr.Patrick Catanzarit.

    • Patrick Catanzariti

      Thanks!! Really glad you enjoyed it :D

  • Chris Ward

    Awesome!!!!

    • Patrick Catanzariti

      Thanks Chris! Got a few more of these Unity based ones coming ;)

  • Matthew Ong

    The tutorial looks really step by step and informative, will update on how it turns out!

    • Patrick Catanzariti

      Yay! Please do :) Hope it helps spur on some fun experiments!

      • Matthew Ong

        It definitely has spurred some on! This tutorial got me into unity, virtual reality, and developing games in general! Thanks so much. I’m currently working on a really simple virtual reality game to test out what I’ve learned, will post when it’s done!

        • Patrick Catanzariti

          Music to my ears Matthew :) Definitely post it up when it’s done, I can’t wait to see it!

  • Matthew Ong

    The tutorial looks really step by step and informative, will update on how it turns out!

  • AsenT

    Patrick you are my hero! I admire your tutorial for getting started in the VR world with Unity and Android Cardboard SDK. I will fully commit myself to the VR development because of you and from now on I am stopping the development of all my other unity projects, because they are now(I realised it now) old technik for visualising the reality. The revolution of VR has began! Thank you

    Regards,
    AsenT

    • Patrick Catanzariti

      Yay! I’m very glad you’re committed to getting into the ever exciting world of VR :) Chances are that some of your projects will be portable to a VR experience too. Or you can build projects which work in both VR and non-VR environments (just provide a different camera and control method).

    • Patrick Catanzariti

      Yay! I’m very glad you’re committed to getting into the ever exciting world of VR :) Chances are that some of your projects will be portable to a VR experience too. Or you can build projects which work in both VR and non-VR environments (just provide a different camera and control method).

  • Harry Dimitriadis

    Wow, a really helpful tutorial. well done !
    I just wanted to ask how can i add a 360 photo instead of the Skyshere asset you use.? or add a totally sherical asset ( like a ball )

    • Patrick Catanzariti

      Hi Harry! Thank you for the kind words, I’m glad you found it useful! The first thing I’d try is using the actual Unity skybox feature in the later version of Unity. I used it in this tutorial for the sky: http://www.sitepoint.com/web-apis-and-iot-in-unity/. I think that might work for a 360 photo that wraps around your scene.

      As for a ball, that’d work just the same way as adding anything else to the scene, just add a sphere into the Unity scene. If you’re looking for it to wrap all around you, I’d consider the skybox idea I mentioned. I’ve also seen methods of doing 360 photos via a sphere in front of each side’s camera. I’m not sure if that works with the Google Cardboard plugin but it might be worth trying out if the skybox doesn’t work. I’ll have to give it a go sometime and write a follow up article!

  • Harry Dimitriadis

    Wow, a really helpful tutorial. well done !
    I just wanted to ask how can i add a 360 photo instead of the Skyshere asset you use.? or add a totally sherical asset ( like a ball )

  • pm

    Fantastic!
    I’m struggling with three for days and you wrote the perfect tuto to revive the VR flame :)
    I didn’t know Unity was so brilliant for building to Android.

    BTW, how to get the zoom in the middle to adapt stereo? Because it’s too separated for me.
    Thanks

    • Patrick Catanzariti

      Yay! Let that VR flame burn brightly :D I’m not quite sure what you mean by getting the zoom to adapt stereo? Could you elaborate a bit?

  • http://www.mobileapptelligence.com/ kailash mobileapptelligence

    In May 2015, Google reported that it received more search queries on mobile devices than on desktop devices. The shift has incentivized Google to reward websites that render well on mobile devices. App development companies Mobileapptelligence [dot] com, are scaling up their infrastructure and delivery capability to meet up the demand.

  • Sam

    Hi Patrick, I am having a problem that the build on my phone looks very uncomfortable when I put it in the cardboard or any other HMD (v1, v2, Homido, Freefly) its like the distance between the eyes is off or perhaps a little cross eyed. I am building to a Samsung S6

    • Patrick Catanzariti

      Hmmm that’s an interesting one! If you go into your “Main Camera” object within CardboardMain > Head, what are the positions of Main Camera Left and Main Camera Right? They are -0.03 and 0.03 respectively for me. Maybe its something that’s gotten messed up there… Does it look alright when you’re previewing it in Unity before exporting?

    • Patrick Catanzariti

      Another thing I’m starting to read about from the New York Times giving out a tonne of Google Cardboard devices earlier this week – some people actually experience them a bit differently and have this issue where they can’t see these experiences clearly. Before spending too long debugging – check that it looks off for someone else too! Or try a different VR app and compare? Just a thought!

    • Carlos Gabriel Figueroa Ortiz

      Im having the exact same problem, in the Editor it shows perfectly the Stereoscopic effect, but none of my devices are working, no effect whatsoever i’ve tried everything…

      • Patrick Catanzariti

        As in the stereoscopic effect isn’t happening on those devices? Or it just looks off a bit? I wonder what might be causing this if it isn’t just a personal thing where some people’s eyes aren’t getting the effect right…

        • Carlos Gabriel Figueroa Ortiz

          Is not happening, apparently it renders both left and right eye on both, whit no difference between them, i have tried everything, i only have a skybox, some UI
          and one 3DSphere,…

          • Patrick Catanzariti

            That is weird… I’ll be doing a bit more Unity and VR stuff for an upcoming SitePoint article, so I’ll keep an eye out for this and see if I see the bug too when I’m testing.

    • Patrick Catanzariti

      One thing that might help — each Cardboard device should have a QR code thing that you can scan to set up the settings for your device correctly. I had the same issue when switching to a new Cardboard headset — everything was cross eyed till I changed the settings :)

  • gaga

    Thank you very much!this article help me a lot~

    • Patrick Catanzariti

      I’m really glad it did! That’s the aim of the game :)

  • IT Stud

    Hey, thanks for this. I’m doing a vr game on android and at 26.12.2015 it’s will be on http://informatyka.am.szczecin.pl

    • Patrick Catanzariti

      You’re welcome! Can’t wait to check it out :)

  • Arif Firmansyah

    Hi, thanks for your tutorial. But i have a problem, where i put this code?In Lamp script?

    public void rubLamp() {
    GetComponent().Emit(10);
    }

    If I put this code in lamp script, when I build and run the code error message appear.
    Thanks

    • Patrick Catanzariti

      Hi Arif!

      That function does indeed go into the Lamp.cs script. If you’re having difficulties, maybe try downloading the project from GitHub and comparing: https://github.com/sitepoint-editors/GoogleCardboardUnitySDKDemo.

      PatCat

      • Arif Firmansyah

        I’ve tried your project from github, when I build the code runs smoothly. but when I run on android, only a blank screen appears. What is wrong?

        • Patrick Catanzariti

          Check that you are building for the right version of Android that matches your device, if that seems correct, maybe check a different Android device to see if the problem persists. It could be a settings thing, it’s a hard one to debug from afar :/

      • ceethal piyus

        It was really working :) Thank you sir

  • Patrick Catanzariti

    Fantastic!!! I’m travelling on 4G (and have managed to use up most of my internet usage), so it is scheduled to be downloaded soon :D The screenshots look great!

    I’ll also share this on my weekly newsletter at DevDiner.com :)

  • Patrick Catanzariti

    Fantastic!!! I’m travelling on 4G (and have managed to use up most of my internet usage), so it is scheduled to be downloaded soon :D The screenshots look great!

    I’ll also share this on my weekly newsletter at DevDiner.com :)

  • Jade

    This is incorrect; see above comment.

  • Jade

    This is incorrect; see comment above.

  • Patrick Catanzariti

    Hi Jade! Yes, you are correct, it should be “ParticleSystem” and not “particlesystem”. The CMS auto corrected it secretly for me as well when the article was posted. I’ve updated it and shall hope it remains in the correct case now :)

  • Andy

    Great tutorial Patrick! I’ve tried following it and it’s mostly working, but the target isn’t showing up when looking over the lamp. Any idea what I’m missing?

    • Patrick Catanzariti

      Hi Andy! Chances are you’re missing the “Physics Raycaster” component within the Google Cardboard SDK’s camera that I mention you’ll need to add – check that’s there and in the right place.

  • Felix

    Thx so much for this awsome tutorial! My first steps into VR has begun :)

    I’m having 2 problems though:
    1. The Target doesn’t show up in front of any object, only behind/partly inside them. I’ve tried with the Accurcy sprite as well as with a custom one…

    2. The Target shows up on every object, (it works fine if I have the colliders turned off on all other objects but I do need their colliders) so is there a away to lock the GazeInputModule to only a specific object?

    • Patrick Catanzariti

      I’m not sure quite how you could fix the target object to always be in front – maybe enlarging the containing object and putting the sprite at one end? I haven’t tested this, so this is just an idea!

      In terms of the second one, maybe look into making the raycase ignore some objects – http://answers.unity3d.com/questions/416906/make-raycast-ignore-some-objects.html (again, I haven’t tried this myself yet but let me know how it goes!)

  • anuragji

    Thanks for the tutorial – its a great introduction to Google Cardboard and Unity – to both of which I am rather new. I have a quick question: it looks like that the script is not getting called correctly. The target appears, when I view the item, but the sky does not change color. Nor does the button appear to be working correctly. How would I go about troubleshooting it? I even modified stareAtLamp() to simply echo a message to console, but not event that gets triggered. The EventTrigger appears to be set correctly though, but I must be missing something.

    • anuragji

      Duh, I realized I added the sphere collider to the EventSystem instead of MagicLamp_LOD2. It’s all working now.

  • Monty King

    One question I have is, if I build with this on Unity, and I want to play my unity game on my windows 8.1 tablet which is x86. Will the stereoscopy and other cardboard stuff work on my tablet? I am intending to turn the tablet into a cardboard device possibly, thanks.

    • Patrick Catanzariti

      Hi Monty! I’m actually not sure whether it would work the same on windows 8.1 as I don’t think the Cardboard SDK is compatible with it. It might do the stereoscopy view as a preview in Unity but I’m not sure if it’ll do the looking around the scene and interactions.

  • Patrick Catanzariti

    Thanks for the tips! I’ll be doing a bunch of stuff in Vuforia, so I’ll keep an eye out for their example whilst I’m there and see how it works :)

    If you find a nice way to have the target always in view, let me know! If I have a chance to play around and find a way, I’ll update with a new comment below. Shouldn’t be too hard right?

  • Pastafarian

    getting a compile error when I try and add the lamp script. I have copied it exactly as you have, what am I missing?

    • Patrick Catanzariti

      I’m not quite sure, i’ve got a sample project on GitHub above, have you compared it to that one? If not, what’s the compile error?

  • vijayenthiran subramaniam

    Hi Patrick, Great tutorial, If I want to bring in a real video feed from another camera (IP camera)how to do it in unity?

  • Patrick Catanzariti

    I wish I knew! I can’t say precisely why the view is as it is but when I looked through in a Google Cardboard device it looked spatially correct to me. Maybe there’s a reasoning behind it that makes sense… I just don’t know it!

  • Justin

    Hi, very good tutorial, it helps a lot! Thanks :)

    The only thing is that, in EventSystem, when you say to add the target to the “Cursor” field of the Gaze Input Module, there is absolutely no “Cursor” field in my GazeInputModule!

    Do you know why? (I have the latest version of Unity, 5.3.1 Personal Edition)

  • Bestenator Bestenator

    I have the same problem.

  • Bestenator Bestenator

    And offcourse, :-) the tutorial is great. Thankyou.

  • Bestenator Bestenator

    The problem is that we are using the 0.6 version of the carboard sdk, In this version the GazeInputModule.cs has been changed. I don’t know why.

    I think that Patrick made the tutorial with the cardboar version 0.52. In this version the cursor variable exist.

    • Patrick Catanzariti

      Ah! That must be why! I’ll need to write a follow up article with how to do it on the new Cardboard SDK – is it just the cursor variable that differs? Have you managed to still get it to work or is it incompatible with the gaze functionality in the article?

      • Bestenator Bestenator

        Hi Patric, sorry. I’m making some test with this. I think that the rest of the gaze funcionality are working. Thankyou.

        • Justin

          Exactly, the Gaze Input Module is working! When looking at the lamp and then away, the sky changes… Only the target does not appear, because we could not set it as a Cursor element!

          Do you have any idea on how to do that with the new Cardboard SDK?

          • Bestenator Bestenator

            Hi all. Reading the cardboard sdk documentation. I just found this:

            “CardboardReticle

            This prefab adds an interactive gaze based reticle. It behaves as a small pointer in world space that correctly casts itself into the scene where the user’s focal focus is expected to be”.

            Adding a CardboardReticle Prefab to the MainCamera object is the solution. Now you can delete de Target gameObject , etc..

            Regards.

          • Justin

            Thank you,that should be the solution! So it has to be done in the MainCamera…

            I will try it soon on my project ;)

          • Patrick Catanzariti

            Hi all! I’ve got an update on how to handle the new way of doing reticles in the SDK — http://www.sitepoint.com/what-in-the-world-is-a-reticle-cardboard-unity-sdk/

  • http://compbcn.es/ qant

    Cant see more then half of images…. open a console and many jpg is 404… wtf?

    • Patrick Catanzariti

      I think we had an issue with images appearing across the board here, they should all be a-okay now :)

  • Mustakim Sunny

    Hello, I was trying to load the android sdk asset package, but everytime it keeps saying ‘couldn’t decompress the package’, can anyone kindly tell me how to resolve this?

  • Patrick Catanzariti

    I think we had an issue with images appearing across the board here, they should all be a-okay now :) It wasn’t your connection!

  • ceethal piyus

    i tried your script ,and got the output but i want to play an audio clip separately for each time the sky box changes its pattern.Am attaching my script here .As soon as possible please reply the corrected script.

    using UnityEngine;
    using UnityEngine.AudioSource;
    using UnityEngine.EventSystems;
    using System.Collections;
    public class Lamp : MonoBehaviour {
    public Renderer skybox;
    public Material sky1;
    public Material sky2;
    public Material sky3;
    public Material sky4;
    private AudioSource source;
    public AudioClip audio1;
    public AudioClip audio2;
    public AudioClip audio3;
    public AudioClip audio4;

    private int currentSky = 0;

    void Start () {
    }

    public void rubLamp() {
    GetComponent().Emit(10);
    }
    void Awake(){
    source = GetComponent();
    }
    public void stareAtLamp() {
    switch (currentSky) {
    case 0:
    skybox.material = sky2;
    source.Play(audio1);
    currentSky++;
    break;
    case 1:
    skybox.material = sky3;
    ssource.Play(audio1);
    currentSky++;
    break;
    case 2:
    skybox.material = sky4;
    source.Play(audio3);
    currentSky++;
    break;
    case 3:
    skybox.material = sky1;
    source.Play(audio4);
    currentSky = 0;
    break;
    }
    }
    }

  • Patrick Catanzariti

    The latest SDK has changed a bit of this. I’ve just posted up a guide to the changes — http://www.sitepoint.com/what-in-the-world-is-a-reticle-cardboard-unity-sdk/

  • doctsh Kumar

    thank you so much for a wonderful tutorial. It helped me to build my app within a day. I have a doubt. Could you please help me solving?
    I have a 3d object in the scene. This object has several parts with description text. I want the text appear when the focus is on each part. How to achieve this? Thanks in advance

  • Khagesh Desai

    Hi Patrick Catanzariti,
    it’s a great tutorial. thanks for shearing it. but i’m facing some problem…
    1) the app is not set up with all android mobiles in some devices it looks doubled ex. Samsung edge s6 and LG 5x.
    2) i want to make gaze pointer visible all time and change on hover.
    3) and how to magnet click woks.
    thanks in advance.

  • Tushar Nath

    Hi, thanks much for the awesome tutorial. Have you tried creating VR for Google Cardboard for a FBX file exported from 3DSMax or Autodesk Revit?

  • Monica

    Hello, when I try to click on “Player Settings” on the Build Settings window the button is disabled for Android, it only allows me to click on it for Web Player, what might be wrong, what can I do to use it on Android?

    • Katherine Martinez

      by default unity only gives you the webplayer option, so you’ll have to update/reinstall unity and make sure you include android when you’re picking what parts of unity to install

  • Atul Sandilya

    hello sir, as per google cardboard sdk they providing us the
    CardboardRecticle and also it get shrink when we look a game object. but
    i need to implement click when user is looking the same object for some
    10 or 20 seconds. so please tell me how can i implement that.

Recommended

Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

Get the latest in Front-end, once a week, for free.