Markerless Augmented Reality Tutorial

This tutorial goes through how to do markerless augmented reality for beginners using the Unity 3D game engine. The app we will be making will work for Android or IOS mobile devices. Most of my videos have been using Vuforia so I wanted to do something a little different. It seems the standard in Markerless AR is the Kudan plugin but I decided against that because it requires a wildcard provisioning profile to build out (meaning you need a $100 dev license from apple). Instead we are going to do a augmented reality app from scratch and augment everything with respect to the phones orientation using it's gyro.

Full Text Instructions can be found here: Virtual Reality Skateboard



webCamScript.cs


using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class webCamScript : MonoBehaviour {

  public GameObject webCameraPlane; 
  public Button fireButton;


  // Use this for initialization
  void Start () {

    if (Application.isMobilePlatform) {
      GameObject cameraParent = new GameObject ("camParent");
      cameraParent.transform.position = this.transform.position;
      this.transform.parent = cameraParent.transform;
      cameraParent.transform.Rotate (Vector3.right, 90);
    }

    Input.gyro.enabled = true;

    fireButton.onClick.AddListener (OnButtonDown);


    WebCamTexture webCameraTexture = new WebCamTexture();
    webCameraPlane.GetComponent<MeshRenderer>().material.mainTexture = webCameraTexture;
    webCameraTexture.Play();




  }


  void OnButtonDown(){

    GameObject bullet = Instantiate(Resources.Load("bullet", typeof(GameObject))) as GameObject;
    Rigidbody rb = bullet.GetComponent<Rigidbody>();
    bullet.transform.rotation = Camera.main.transform.rotation;
    bullet.transform.position = Camera.main.transform.position;
    rb.AddForce(Camera.main.transform.forward * 500f);
    Destroy (bullet, 3);

    GetComponent<AudioSource> ().Play ();


  }
  
  // Update is called once per frame
  void Update () {

    Quaternion cameraRotation = new Quaternion (Input.gyro.attitude.x, Input.gyro.attitude.y, -Input.gyro.attitude.z, -Input.gyro.attitude.w);
    this.transform.localRotation = cameraRotation;
  
  }
}



enemyScript.cs


using UnityEngine;
using System.Collections;

public class enemyScript : MonoBehaviour {



  // Use this for initialization
  void Start () {

    StartCoroutine ("Move");
  }

  // Update is called once per frame
  void Update () {

    transform.Translate(Vector3.forward * 3f * Time.deltaTime); 
  }

  IEnumerator Move() {


    while (true) {
      yield return new WaitForSeconds (3.5f);
      transform.eulerAngles += new Vector3 (0, 180f, 0);
    }
  }
}



collisionScript.cs


using UnityEngine;
using System.Collections;

public class collisionScript : MonoBehaviour {

  // Use this for initialization
  void Start () {

  }

  // Update is called once per frame
  void Update () {

  }

  //for this to work both need colliders, one must have rigid body (spaceship) the other must have is trigger checked.
  void OnTriggerEnter (Collider col)
  {
    GameObject explosion = Instantiate(Resources.Load("FlareMobile", typeof(GameObject))) as GameObject;
    explosion.transform.position = transform.position;
    Destroy(col.gameObject);
    Destroy (explosion, 2);


    if (GameObject.FindGameObjectsWithTag("Player").Length == 0){

      GameObject enemy = Instantiate(Resources.Load("enemy", typeof(GameObject))) as GameObject;
      GameObject enemy1 = Instantiate(Resources.Load("enemy1", typeof(GameObject))) as GameObject;
      GameObject enemy2 = Instantiate(Resources.Load("enemy2", typeof(GameObject))) as GameObject;
      GameObject enemy3 = Instantiate(Resources.Load("enemy3", typeof(GameObject))) as GameObject;

    }

    Destroy (gameObject);


  }

}




Hey !!!

about me.

My name is Matthew and I attend the University of Pittsburgh. Currently I am a Senior, going for a bachelors in Information Science with a minor in computer science. I work as a food server when I am not at school, but would love something a little more technical, if anyone is hiring.

  • Pittsburgh, PA
  • MatthewHallberg@gmail.com

Drop me a line

For free lance work
or consulting.
Send inquiries to:
MatthewHallberg@gmail.com