How to Make a Virtual Reality Game Tutorial

This Tutorial will walk you through making your first virtual reality app in Unity 3D with the GoogleVR plugin. This game will work for IOS and Android so it will run with traditional Google Cardboard headsets as well as the new Daydream VR headset. This will not require any previous experience with VR development or Unity 3D so I will try to make all the instructions as clear and beginner friendly as possible. The virtual reality game we will be making is a first person zombie shooter. Full Text Instructions can be found here: Virtual Reality Skateboard



playerScript.cs


using UnityEngine;
using System.Collections;

public class playerScript : MonoBehaviour {

  //declare GameObjects and create isShooting boolean.
  private GameObject gun;
  private GameObject spawnPoint;
  private bool isShooting;

  // Use this for initialization
  void Start () {

    //only needed for IOS
    Application.targetFrameRate = 60;

    //create references to gun and bullet spawnPoint objects
    gun = gameObject.transform.GetChild (0).gameObject;
    spawnPoint = gun.transform.GetChild (0).gameObject;

    //set isShooting bool to default of false
    isShooting = false;
  }

  //Shoot function is IEnumerator so we can delay for seconds
  IEnumerator Shoot() {
    //set is shooting to true so we can't shoot continuosly
    isShooting = true;
    //instantiate the bullet
    GameObject bullet = Instantiate(Resources.Load("bullet", typeof(GameObject))) as GameObject;
    //Get the bullet's rigid body component and set its position and rotation equal to that of the spawnPoint
    Rigidbody rb = bullet.GetComponent<Rigidbody>();
    bullet.transform.rotation = spawnPoint.transform.rotation;
    bullet.transform.position = spawnPoint.transform.position;
    //add force to the bullet in the direction of the spawnPoint's forward vector
    rb.AddForce(spawnPoint.transform.forward * 500f);
    //play the gun shot sound and gun animation
    GetComponent<AudioSource>().Play ();
    gun.GetComponent<Animation>().Play ();
    //destroy the bullet after 1 second
    Destroy (bullet, 1);
    //wait for 1 second and set isShooting to false so we can shoot again
    yield return new WaitForSeconds (1f);
    isShooting = false;
  }
  
  // Update is called once per frame
  void Update () {
  
    //declare a new RayCastHit
    RaycastHit hit;
    //draw the ray for debuging purposes (will only show up in scene view)
    Debug.DrawRay(spawnPoint.transform.position, spawnPoint.transform.forward, Color.green);

    //cast a ray from the spawnpoint in the direction of its forward vector
    if (Physics.Raycast(spawnPoint.transform.position, spawnPoint.transform.forward, out hit, 100)){

      //if the raycast hits any game object where its name contains "zombie" and we aren't already shooting we will start the shooting coroutine
      if (hit.collider.name.Contains("zombie")) {
        if (!isShooting) {
          StartCoroutine ("Shoot");
        }

      }
        
    }
      
  }
}




zombieScript.cs


using UnityEngine;
using System.Collections;

public class zombieScript : MonoBehaviour {
  //declare the transform of our goal (where the navmesh agent will move towards) and our navmesh agent (in this case our zombie)
  private Transform goal;
  private NavMeshAgent agent;

  // Use this for initialization
  void Start () {
  
    //create references
    goal = Camera.main.transform;
    agent = GetComponent<NavMeshAgent>();
    //set the navmesh agent's desination equal to the main camera's position (our first person character)
    agent.destination = goal.position;
    //start the walking animation
    GetComponent<Animation>().Play ("walk");
  }


  //for this to work both need colliders, one must have rigid body, and the zombie must have is trigger checked.
  void OnTriggerEnter (Collider col)
  {
    //first disable the zombie's collider so multiple collisions cannot occur
    GetComponent<CapsuleCollider>().enabled = false;
    //destroy the bullet
    Destroy(col.gameObject);
    //stop the zombie from moving forward by setting its destination to it's current position
    agent.destination = gameObject.transform.position;
    //stop the walking animation and play the falling back animation
    GetComponent<Animation>().Stop ();
    GetComponent<Animation>().Play ("back_fall");
    //destroy this zombie in six seconds.
    Destroy (gameObject, 6);
    //instantiate a new zombie
    GameObject zombie = Instantiate(Resources.Load("zombie", typeof(GameObject))) as GameObject;

    //set the coordinates for a new vector 3
    float randomX = UnityEngine.Random.Range (-12f,12f);
    float constantY = .01f;
    float randomZ = UnityEngine.Random.Range (-13f,13f);
    //set the zombies position equal to these new coordinates
    zombie.transform.position = new Vector3 (randomX, constantY, randomZ);

    //if the zombie gets positioned less than or equal to 3 scene units away from the camera we won't be able to shoot it
    //so keep repositioning the zombie until it is greater than 3 scene units away. 
    while (Vector3.Distance (zombie.transform.position, Camera.main.transform.position) <= 3) {
      
      randomX = UnityEngine.Random.Range (-12f,12f);
      randomZ = UnityEngine.Random.Range (-13f,13f);

      zombie.transform.position = new Vector3 (randomX, constantY, randomZ);
    }

  }

}





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