Collecting Kittens in Unity with KittenVR

Blog Posts ,Programming ,Tutorials ,Unity ,Virtual Reality
May 19, 2015

Kitten VR began as an experiment with building low poly environments in Unity 5, and quickly turned into a simple application with a lot of potential. My goals with Kitten VR are twofold: to have a simple, easy to play mini-game for use with virtual reality devices that I can use as a basis for testing new VR SDKs, and to document the process for those interested in learning how to build applications that take advantage of today’s VR ecosystem themselves.KittenVR

Right now, I’m using the Oculus Runtime 0.5.1 and the Unity plugin for 0.4.4 (newer versions don’t work well with Unity 5, so I’m improvising for the time being). I’m hoping to have downloadable versions available soon!

For detailed information about what I’m building, read on!

Environment
Right now, Kitten VR is a one-stage application where the user helps a cat find all 7 of her missing kittens. The environment is a Unity scene created with a simple Terrain element and a Plane GameObject, both of which feature bright colors and textures from the Cartoon Forest Environment asset package, which can be purchased on the Asset Store.

Characters
There are 7 collectable kittens hidden within the scene, which are animated prefabs from a free Kitten package. One additional kitten is a basic NPC character, which tells the user their goal and cannot be collected. There is also a character controller that uses the FPS Character Controller prefab with the Oculus Camera Rig replacing a normal camera.

KittenController.cs
The user can get a hint for what the general game play is by clicking on the closest kitten at the spawn point. This kitten has a `KittenController` script component, written in C#, which detects the `OnClick` behavior and displays a message based on how many kittens have been found. The script uses a `RaycastHit` and `Ray` to determine if the click was on the kitten target. If the player hasn’t picked up any of them, the lead kitten will inform the player of their mission. Otherwise, the kitten will display how many more cats need to be found. After all kittens are discovered, the game will reset after the player clicks the parent kitten.

 using UnityEngine;
 using System.Collections;
 
public class KittenController : MonoBehaviour {
 public bool isVisible = false;
 private float timer = 0.0f;
 public PlayerController playerStats;
 private string message;
 
// Use this for initialization
 void Start () {}

// Update is called once per frame
 void Update () {
 if (Input.GetMouseButtonDown (0)) {
    RaycastHit hit;
    Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    if (Physics.Raycast (ray, out hit))
     if (hit.transform != null) {
      if (hit.transform.gameObject == this.gameObject) {if(playerStats.kittens_collected == 0)
 {
 message = "Help! My kittens have gone missing! Can you help collect all 7 of them?";
 isVisible = true;
 }
 else if(playerStats.kittens_collected == 1)
 {
 message = "You've found one kitten so far! Just 6 more to go!";
 isVisible = true;
 }
 else if(playerStats.kittens_collected < 7)
 {
 message = "You've found " + playerStats.kittens_collected + " kittens so far! Just " + (7 - playerStats.kittens_collected) + " left to go!";
 isVisible = true;
 }
 else
 {
 message = "You've found them all! Thank you so meouch!";
 isVisible = true;
 System.Threading.Thread.Sleep(2000);
 Application.LoadLevel(Application.loadedLevel);
 }}}}
 if (isVisible) {
 timer++;
 }
 if (timer > 300f) {
 isVisible = false;
 timer = 0f;
 }}
void OnGUI()
 {
   if (isVisible) {GUI.Box (new Rect (Screen.width / 2 - 290, Screen.height / 1 - 100, 600, 80), message);
  }
 }
}

PlayerController.cs

PlayerController.cs is another C# script attached to the player to track the number of kittens that the player has found. It has two functions that are called in the `Update()` method: `CheckPosition()` and `MouseClickListener()` The first method checks to make sure the user hasn’t jumped off the game board (if so, the level will reset with the `Application.LoadLevel(Application.loadedLevel);` call) and the second uses another `RaycastHit` to see if a kitten has been touched. If so, the kitten is removed from the scene and added to the player’s basic ‘inventory’.

 using UnityEngine;
 using System.Collections;

public class PlayerController : MonoBehaviour {

public int kittens_collected = 0;
 /**
 * Use this for initialization
 **/
 void Start () {}
/**
 * Update is called once per frame.
 * Check for various game play components.
 **/
 void Update () {
 MouseClickListener ();
 CheckPosition ();
 }
/**
 * Check and see if the user has jumped off of the level.
 * If so, reset the level.
 **/
 void CheckPosition()
 {
 if (this.gameObject.transform.position.y < 0) {
 Application.LoadLevel(Application.loadedLevel);
 }
 }
/**
 * A function to check if the player has clicked on a kitten.
 * Eventually update this with a different, more interactive input type.
 **/
 void MouseClickListener()
 {
 if (Input.GetMouseButtonDown(0)) {
 RaycastHit hit;
 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 if (Physics.Raycast(ray, out hit))
 if (hit.transform != null) {
 if(hit.transform.gameObject.tag == "kitten")
 {
 kittens_collected++;
 Destroy(hit.transform.transform.gameObject);
 Debug.Log("You've collected " + kittens_collected + " kittens!");
 }}}}
}

The full code can be found on GitHub here. This is a living application and will have changes updated regularly with new features and functionality.

Interested in learning more, but unsure of where to start? Go from zero to application with my notes on building your first game from scratch with Unity 5!

Related Posts

Leave a Reply