Complete Astro Dude game redo to AR game

Project preparation

  1. Create new empty project in Unity 3D

  2. Download Astro Dude project from Asset Store https://www.assetstore.unity3d.com/en/#!/content/1287

  3. Import Astro Dude game to your project.

  4. Download Vuforia SDK for Unity from here and import it to your project

  5. Download Live Game Board SDK for Unity and Vuforia from HERE and import it to your project

  6. The project from asset store is a bit outdated so lets fix some set up first.

    • Open scene Scenes/LostInSpace

    • Change scale of lunar_lander under Spaceship game object to 1 so it is right size again

    • Shadows are quite offset from its place. Open LightMapping tab (or in menu->Window), don't change anything these just select all objects in hierarchy and press Bake Selected in inspector window and wait until shadows are rebaked properly again.

    • Astro dude (our character) is also badly rotated. Lets fix that by setting rotation of GO player under Character to 0.

    • The scene is a bit dark. In scene view click on moon surface and set main color of the material to a bit lighter to fit your feelings

    • Game is build specifically for Sony Xperia so lets disable check for its screen resolution. Select game object Control and deactivate Setup GUI component.

    • Playing the game at this stage throws error when trying to call native Android class. Comment out entire block Using{} in CurrentConfig get{} in XperiaInput.cs script

    • When you start the game it shows texture with text buttons on it. In Menu GUI script there is particular mapping of rectangular areas to these buttons. So when you mouse click on the button action is taken. This mapping works only with particular screen resolution. Changing this is out of the scope of this tutorial so lets just modify MenuGUI script settings in inspector on Control game object and set Start Button Rect x and y to 0 and W and H to something big like 3000. Now clicking anywhere will start the game.

    • When you try to start the game it gives an error In script XperiaInput.cs, open it and add return true; at the beginning of get block in KeypadAvailable method.

    • Now you can play the game a little with mouse and keyboard to see how it works and then we'll jump on actual Augmented reality.

Enabling Augmented Reality in the game

  1. Go to Assets/Plugins/Ligabo/Prefabs and drag the LigaboCamera prefab into your scene and set its X and Z to 0 so it nicely points down to our scene. Delete or disable original Main Camera under Character game Object. LigaboCamera must be the main camera in the project. Disable also CameraControl script on Character game object in the scene as we do not want this to control camera any more. 

  2. Add LigaboTarget located in the same folder as LigaboCamera. Drag LigaboTarget to Hierarchy view. Set its position to 0. When you have LigaboTarget object selected, in inspector on Image Target Behavior script set drop down Data to empty and then back to Ligabo (needed just due to some funny state of this after putting prefab into scene)

  3. Create empty GameObject, rename it to Content. And set its position to 0 and scale to 1.

  1. In the hierarchy view, select all game objects that represent game scene which you want to be Augmented and drag them under Content object. These are Character, all Crystals, Flag, Spaceship and Surface. We can leave out Control as it does not represent any content. We leave out also Sunlight and Earthlight (you can enable it if you want) because in AR only camera moves around the target game object but target and its content remain in place so lighting can stay where it is.

  2. Now move Content object under LigaboTarget.

  3. Before we start the game with AR mode, we need to disable skybox as this makes camera feed not visible. Go to Render Settings in Edit menu and change Skybox material to none.

When you now start the game in editor, you should see feed from camera attached to your computer. (You need Unity Pro for this. If you don't you'll need to build to your mobile device to test. In case camera feed won't show in editor player, check settings of web cam behavior script attached to LigaboCamera object. Make sure your camera is listed and selected). As you can see game scene is not visible after starting. Take camera and point it to Live Game Board (if you do not have one printed yet, go to www.livegameboard.com/shop/ and download one for self print there and print it). Game scene shows up and is Augmented in camera feed sitting on the game board.

We have done basic set up of augmentation how ever the game need some special awareness of game board tracking state. When board is not being tracked game scene is hidden, it must be otherwise it would appear just hanging on one place when tracking is lost. However when game is not visible player cannot play it so the game must be properly paused until board is tracked again.

Making the game AR aware

  1. Drag LiGaBo prefab located in the same folder as LigaboCamera to scene.

    LiGaBo prefab has attached main LigaboState class which does provide information about tracking state. It also provide GUI layer with instructions for player to point device camera to Live Game Board when he is not doing so.

When game is started we need to check if game board is being tracked before we start the game. If it is not, Ligabo will instruct user to point device camera to live game board.

  1. Open DemoControl script attached to Control game object in the scene. Lets modify it to recognize when user started the game so we can start Ligabo which will then show instructions when needed.

    Add on top of the script

    using Ligabo;

    Add object property

    public bool started=false;

    then in Update() function replace these lines

    // Flip game and menu/credits GUI based on game pause state //
    gameGUI.enabled = Time.timeScale > 0.0f;
    creditsGUI.enabled = menuGUI.enabled = !gameGUI.enabled;

    With these

    if (started && LigaboState.status != LigaboState.Status.Running){
    LigaboState.status = LigaboState.Status.Running;
    creditsGUI.enabled = menuGUI.enabled = false;
    }
    if (started && LigaboState.statusTracking == LigaboState.StatusTracking.Tracking)
    {
    Time.timeScale = 1.0f;
    gameGUI.enabled=true;
    }
    else if (started){
    Time.timeScale = 0.0f;
    }
  2. Now when Start is pressed we need to tell demo control that we should start. So Instead of turning timeScale into 1 lets change started value to true. In MenuGUI.cs script attached to Control GO replace this line in case EventType.MouseDown block

    Time.timeScale = 1.0f;

    with

    GetComponent().started=true;

When you start the game now, GUI for player with instructions to point camera to live game board appear.

As soon as board is detected, instructions disappear and game is started. When during the game board tracking is lost, game pauses. When tracking is available again game is automatically resumed.

Adding character control

In Augmented reality play mode specific character control is required. There are different control modes which are suitable for AR. In this particular case we'll use on screen joystick to control character movements.

  1. In Project hierarchy, in folder Ligabo/ in prefabs folder you can see LiGaBOMoveJoystick prefab. Drag it to hierarchy view to add it to the scene. Set its x position to 0.8 to have it aligned on the right.

  2. Select Character game object under Content and attach to it script FollowJoystick located in Ligabo/Scripts/controllers folder. Set its Move Force to 50.

  3. Disable Move Controller script on Character. This was controlling also some physic settings so lets adjust this manually. Set in rigidbody component angular drag and drag to value 5 and under Constrains activate rotation constrains for X and Z axis. Use gravity On and is Kinematic Off.

    Also in Player.cs script change out these 2 lines in Respawn() method, so move controller won't get activated again

    //animationController.state = CharacterState.Falling;
    //movementController.enabled = true;
  4. Lets correct one wrong statement in Meteorite.cs which causes every meteorite to kill our character regardless where it falls down. Change this line in OnTriggerEnter()

    Player player = other.transform.root.GetComponentInChildren();

    to

    Player player = other.GetComponentInChildren(); 
  5. For augmented reality play mode the planet is quite big in compare to character and crystals. Lets make them a bit bigger so it will fit our needs. Select all crystals in Content GO and set scale to 2 in inspector tab. Do the same for Player GO under Character. For Character GO also adjust Capsule Collider, set its radius to 1.5, height to 3.5 and Y position to 1.

    Character for some reason falls down a bit when starting the game, just set Player GO under character position Y to 1.8 to correlate that offset. And change State of Animation Controller script on Character GO to Normal.

Start the game. Now you can control the character by joystick and run around. Note that Joystick works only with touches so either run it on a phone or use Unity remote.

Now lets finish some more stuff to complete the game.

  1. Crystals are originally picked up by pressing a key. We do not want that here, so we will let the astro dude to pick it up just going close to it. In Update() function of ActionController.cs script comment out condition to interact with crystals only when key is pressed like this

    //if (Input.GetKeyDown (InteractKey))
    //{
      Interact ();
    //}
  2. Also we need to update referencing hierarchy when checking for Active Object to match the correct one. In same script in both functions OnTriggerEnter and OnTriggerExit change this line

    ActiveObject activeObject = other.transform.root.GetComponentInChildren();

    to

    ActiveObject activeObject = other.transform.GetComponentInChildren();
  3. You can notice that when you pick up some crystals, and you point camera out of the board so tracking is lost and then point back to get the scene again, crystals which were picked up appear again as not picked up. This is because when tracking is lost all object of the scene below LigaboTarget are hidden by disabling MeshRenderer. When tracking is established again MeshRenderers are set active again. So is the MeshRenderer of crystals including those which were picked up before. To prevent this, we need to change the way Crystals shows their state. Instead of disabling mesh renderer when picked up, we disable entire game object with that mesh renderer.

    In Crystal.cs UpdateVisuals() method change these lines

    availableVisuals.enabled =!spent;
    spentVisuals.enabled = spent;

    to

    availableVisuals.gameObject.SetActive(!spent);
    spentVisuals.gameObject.SetActive(spent);

We are pretty much done, enjoy AR Astro Dude!

You can try the game on Android device by installing it from http://www.livegameboard.com/apps/