Project
Roll-a-ball
Collecting, Scoring and Building the Game
Tutorial
Beginner
45 Mins
Unity Technologies
Overview
Summary
The Roll-a-ball Project is a simple rolling ball game that teaches you many of the principles of working with Unity.
The 4th of 4 tutorials in the project shows how to create the "Pick Up" collectables, place them on the play area, add a script enabling the ball to pick up those collectables, and make a simple scoring system. Finally, we demonstrate how to create a "build" of the game that will run outside of the Unity Engine.
Roll-a-ball tutorials:
  1. Introduction to Roll-a-ball
  2. Environment and Player
  3. Camera and Play Area
  4. Collecting, Scoring, and Building the Game
Language
English
Recommended Unity Versions
5 and Above
1.
Creating Collectable Objects
Creating and placing the "Pick Up" collectables.

Rotator

using UnityEngine; using System.Collections; public class Rotator : MonoBehaviour { void Update () { transform.Rotate (new Vector3 (15, 30, 45) * Time.deltaTime); } }

2.
Collecting the Pick Up Objects
Collecting the pick-up objects; discussing physics, collisions and triggers.
This code reflects the current state of the script as of the end of this particular lesson. This script will continue to grow and mature as the series continues.

PlayerController

using UnityEngine; using System.Collections; public class PlayerController : MonoBehaviour { public float speed; private Rigidbody rb; void Start () { rb = GetComponent<Rigidbody>(); } void FixedUpdate () { float moveHorizontal = Input.GetAxis ("Horizontal"); float moveVertical = Input.GetAxis ("Vertical"); Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical); rb.AddForce (movement * speed); } void OnTriggerEnter(Collider other) { if (other.gameObject.CompareTag ("Pick Up")) { other.gameObject.SetActive (false); } } }

3.
Displaying the Score and Text
Counting, displaying text and ending the game.

PlayerController

using UnityEngine; using UnityEngine.UI; using System.Collections; public class PlayerController : MonoBehaviour { public float speed; public Text countText; public Text winText; private Rigidbody rb; private int count; void Start () { rb = GetComponent<Rigidbody>(); count = 0; SetCountText (); winText.text = ""; } void FixedUpdate () { float moveHorizontal = Input.GetAxis ("Horizontal"); float moveVertical = Input.GetAxis ("Vertical"); Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical); rb.AddForce (movement * speed); } void OnTriggerEnter(Collider other) { if (other.gameObject.CompareTag ( "Pick Up")) { other.gameObject.SetActive (false); count = count + 1; SetCountText (); } } void SetCountText () { countText.text = "Count: " + count.ToString (); if (count >= 12) { winText.text = "You Win!"; } } }

4.
Building the Game
Building the game as a Standalone application.