How to make a Unity3D Ball Roller game

I followed http://unity3d.com/learn/tutorials/projects/roll-a-ball, and updated the steps for the newest Unity3D currently available (4.5). You can try the game and download the project on GitHub.

  1. Create a new 3D project and save a scene in a Assets/Scenes folder
  2. Game object – 3d object – Plane, and rename to Ground
  3. Transform – Reset -> sets to 0,0,0 origin point of the world
  4. Edit – Frame selected or character F
  5. Hierarchy -> Create -> 3D object -> Sphere, and again Transform – Reset and again Edit – Frame selected, rename it to Player
  6. Hierarchy -> Create -> Light -> Directional light (name it Main Light)
    1. Rotation x 30, y 60
    2. Shadow – Soft
    3. Very high resolution
  7. Duplicate Main Light and reverse the Rotation x -30, y -60
  8. Color – some blue, intensity 0.1, no shadows
  9. We use Folders in Project tab to organize stuff and Empty game objects in Hierarchy tab
  10. Create empty object and rename to Lighting
    1. reset transform origin, drag other lighting related object in here
    2. set the y position to 100 so that you move the lighting up (they are based on rotation and not position so that if you place it high up it’s just a matter of “not getting in the way” and they will still work as expected)
  11. Player object -> add Component -> Physics -> Rigidbody
  12. [Moving the player] Create a Scripts folder. Create a script (on the Player object add Component -> New Script) and place it in this folder
    1. Update() – here goes the game code (before rendering the frame)
    2. FixedUpdate() – here goes the physics code (before performing any physics calculation)
    3. To read an axis use Input.GetAxis with one of the following default axes: “Horizontal” and “Vertical” are mapped to joystick, A, W, S, D and the arrow keys. “Mouse X” and “Mouse Y” are mapped to the mouse delta. “Fire1”, “Fire2” “Fire3” are mapped to Ctrl, Alt, Cmd keys and three mouse or joystick buttons.
    4. Add this script:
      var speed : float;
      function FixedUpdate () {
      	var moveHorizontal = Input.GetAxis("Horizontal");
      	var moveVertical = Input.GetAxis("Vertical");
      	
      	var movement = Vector3(moveHorizontal, 0.0f, moveVertical);
      	
      	rigidbody.AddForce(movement * speed * Time.deltaTime);
      }
  13. [Moving the camera] Raise the camera a little and tilt it by setting the rotation. Make it the child of the player (drag the Main Camera to Player object). If you run the game at this point you will see all went bazinga. So, we can’t have it as a child – detach it. What we can do however is attach a script to the camera and offset it based on the position of the player object:
    1. Add script to Main Camera:

      var player : GameObject;
      private var offset : Vector3;
      function Start () {
          offset = transform.position;
      }
      
      function LateUpdate () {   
          transform.position = player.transform.position + offset;
      }
  14. [Adding the walls] – Add new empty object Walls
    1. Create -> 3D object -> Cube (rename to West Wall) and add it as a child to Walls
    2. repeat for other 3 walls
  15. [Adding collectible objects]
    1. Create -> 3D object -> Cube (rename to Pickup)
    2. Reset transform origin
    3. Scale to 0.5 on all axes
    4. Rotate 45° on all axes
    5. Add a script (Rotator):
      function Update () {
      	transform.Rotate(Vector3(15,30,45) * Time.deltaTime);
      }
    6. Create a new folder called Prefabs in the root directory and drag the Pickup object inside it in order to, well, create a Prefab which you can think of as a variable which you can then access with all its parameters. So, when you clone the prefab object, it will have all the behavior that the cloned prefab has.
    7. Create an empty object and drag the prefab inside it
    8. Set orientation to global mode and duplicate a few of the collectibles
  16. [Collision detection]
    1. Add the tag ‘PickUp’ to the Pickup prefab
    2. Add the following function to the Player object script:
      function OnTriggerEnter(other : Collider){
          if(other.gameObject.tag == 'PickUp')
              other.gameObject.setActive(false);
      }
    3. Set Box collider Is Trigger to true in the Pickup prefab and this gives you OnTrigger function
    4. Unity caches all the static colliders – everytime we move, rotate or scale the static colliders, the Unity will recalculate the cache – takes resources! We can move, rotate or scale dynamic colliders and Unity will not cache them. Any game object with a collider and a rigid body is considered a dynamic object. Any game object with a collider and no rigid body is considered a static object
    5. Add a rigidbody to the Pickup prefab. If you start the game now, the Pickup object will fall to the ground because of the Gravity. You can uncheck the Gravity option, but a better solution is to keep it and check the Is Kinematic option.
    6. Static colliders shouldn’t move – walls and floors
    7. Dynamic colliders can move and have a rigidbody attached
    8. Standard rigid bodies are moved using physics forces and Kinematic rigid bodies are moved using transform object
  17. [Counting picked up object]
    1. Add a new variable to the PlayerController script and increment it in the OnTriggerEnter function
    2. Add a new empty game object
    3. Choose “Add Component” -> “Rendering” -> “GUIText”
    4. Transform position (x,y,z) = (0,1,0)
    5. Anchor – upper left
    6. Alignment – center
    7. Pixel offset – 5,5
    8. Font size – 20
    9. Add the GUIText variable to the PlayerController script:
      public var scoreText : GUIText;
      function SetCountText(){
      	scoreText.text = 'Score: ' + score.ToString();
      }
    10. Do the same with the winText (create object, add the public variable in PlayerController script) and drag it to the Player object
  18. [Publishing the game]
    1. File -> Build Settings
    2. Select the target platform
    3. Click on the Switch platform button
    4. Click on the Buildbutton
  19. [Added bonus]
    1. To create a Physics Material select Assets->Create->Physics Material from the menu bar. Then drag the Physics Material from the Project View onto a Collider in the scene.
    2. This way you can make the walls bounce the ball
Written by Nikola Brežnjak