• Home
  • About
    • Hanna's Blog photo

      Hanna's Blog

      I wanna be a global developer.

    • Learn More
    • Email
    • LinkedIn
    • Github
  • Posts
    • All Posts
    • All Tags
  • Projects

[Unity 4] Maze

26 Jul 2018

Reading time ~4 minutes

Game design

  • Maze
  • Background
  • Ball
  • Trap
  • Goal
  • Title
  • Clear

Create maze and background

  • Create floor
    • Create Cube and change Name to Floor
    • Change Scale to (20, 1, 20)
    Maze Floor
    • Create Physic Material and drag and drop to Floor
    • Change Dynamic Friction and Static Friction to 0.2
    Maze Floor
  • Create Point Light
    • Delete Directional Light, create Point Light
    • Change Rosition to (0,12,0), Lange to 50
    Maze Floor
  • Paint floor
    • Save an image to Assets folder
    Maze Floor
    • Drag and drop image to Floor object of hierarchy
  • Change position of Camera
    • Change Position to (0,1,-10)
    Maze Floor
  • Create background
    • Chagne Position to (0,1,-10)
    • Put black color on background
    • Create Plane, Change name to BG, and change Scale to (6,1,6) and Position to (0,0,0)
    Maze Floor

Create walls and barriers

  • Create outer walls
  • Create Cube and change Name to Wall
  • Take same Material with Floor, and change Position to (0,1,-10) and Scale to (20,1,1)
Maze Wall
  • Create Prefab and chang Name to wall
  • Drag and Drop wall in hierarchy to Prefab
Maze Wall
  • Drag and Drop wall of Project to Hierarchy
  • Change Position
Maze Wall
  • Create barriers
  • Create Prefab and change Name to barrier
  • Create Cube and change Position to (0,1,0)
Maze Wall
  • Make barriers
Maze Wall
  • Create Material
  • Save a white image to Assets folder
Maze Wall
  • Put the texture to barrier
  • Change Color in Inspector
Maze Wall
  • Change background Position to (0,-100,0), and Scale to (100,1,100)
  • Change Position of Main Camera
Maze Wall
  • Create Empty Game Object, and change Position to (0,0,0)
  • Put walls, floor, and barriers to under the Object
Maze Wall

Create a ball

  • Create ball
    • Put Ball on start line
    • Paint Ball
    Maze Ball

Create control script

  • GravityController.js
    function Update() {
      Physics.gravity = Quaternion.AngleAxis(Input.GetAxis("Horizontal") * 60.0, Vector3.forward) * Quaternion.AngleAxis(Input.GetAxis("Vertical") * -60.0, Vertor3.right) * (Vector3.up * -20.0);
    }
  • Create empty game object and change Name to Gravity Controller
  • Drag and Drop GravityController script to Gravity Controller object

  • Ball.js
    function Update() {
      rigidbody.WakeUp();
    }

Rig

  • Tilt Rig
    • Create Empty Game Object and change Name to Tilt Rig
    • Change Position to (0,0,0)
    • Put Main Camera, BG, and Point Light to under the Tilt Rig
  • TiltRig.js
    function Update() {
      transform.rotation = Quaternion.AngleAxis(Input.GetAxis("Horizontal") * 10.0, Vector3.forward) * Quaternion.AngleAxis(Input.GetAxis("Vertical") * -10.0, Vector3.right);
    }
  • Drag and Drop TiltRig Script to Tilt Rig Object

  • Result

Create a trap

  • Create warp zone
    • Create Cube and change Name to Trap
    • Select is Trigger of Collider Componenet
    Maze Trap
  • Create Tag
    • Change Name to ball
    • Prefab ball and select ball Tag in Inspector
    Maze Trap
  • Restart point
    • Create Empty Game object and change Name to Respawn Point
    • Change Tag to Respawn
    Maze Trap
  • Trap.js
    functionOnTriggerEnter(other:Collider) {
      if (other.gameObject.tag == "Ball"){
        var respawn : GameObject = GameObject.FindWithTag("Respawn");
        other.gameObject.transform.position = respawn.transform.position;
      }
    }
  • Drag and Drop Script to Trap Object

Create a goal

  • Create goal Trigger
    • Create Cube and change Name to Goal
    • Select is Trigger of Collider in Inspector
    • Create Material to paint and change Name to Goal
    Maze Goal
  • Goal.js
    private var ballCount : int;
    private var counter : int;
    private var cleared : boolean;

    function Start () {
      ballCount = GameObject.FindGameObjectsWithTag("Ball").length;
    }

    function OnTriggerEnter (other : Collider) {
      if(other.gameObject.tag == "Ball"){
        counter++;
        if(cleared == false && counter == ballCount){
          cleared = true;
          Debug.Log("Cleared!");
        }
      }
    }

    function OnTriggerExit(other : Collider) {
      if(other.gameObject.tag == "Ball") {
        counter--;
      }
    }
  • Result
    Maze Goal

Show clear message

  • Goal.js(add)
    function OnGUI() {
      if (cleared == true){
        var sw : int = Screen.width;
        var sh : int = Screen.height;
        GUI.Label(Rect(sw / 6, sh / 3, sw * 2 / 3, sh / 3), "CLEARED!");
      }
    }
Maze Message
  • Add font
    • Drag and Drop font file on the Project
    Maze Message
    • Goal.js
      Add code in first line
        var labelStyle : GUIStyle;
    
Edit OnGUI()
        function OnGUI() {
          if (cleared == true){
            var sw : int = Screen.width;
            var sh : int = Screen.height;
            GUI.Label(Rect(sw / 6, sh / 3, sw * 2 / 3, sh / 3), "CLEARED!", labelStyle);
          }
        }
    
  • Change color, font, size and alignment in Inspector
Maze Message
  • Result
    Maze Message

Create title scene

  • Structure
    Maze Title
  • Save Main Scene
    • File>Save Scene
    • Change Name to Main
    • File>Build Settings…
    • Press Add Current
    Maze Title
  • Create Title Scene
    • File> Save Scene As…
    • Change name to Title
    • Add Title by Press Add Current to add in Scene In Build
    • Put Title in the Top
  • Build Title Scene
    • Delete Barrier, Trap and Goal in Hierarchy View
    Maze Title
    • TitleScreen.js
    var labelStyle : GUIStyle;

    function Update(){
      if (Input.GetButtonDown("Jump")){
        Application.LoadLevel("Main");
      }
    }

    function OnGUI(){
      var sw = Screen.width;
      var sh = Screen.height;
      GUI.Label(Rect(0, sh/4, sw, sh/4), "BALL MAZE", labelStyle);
      GUI.Label(Rect(0, sh/2, sw, sh/4), "Hit Space Key", labelStyle);
    }
  
  • Create Empty Game Object and change Name to Title Screen
  • Drag and drop TitleScreen.js to Title Screen
Maze Title
  • Change action after game clear
    • Goal.js(change)
    function OnTriggerEnter (other : Collider) {
      if(other.gameObject.tag == "Ball"){
        counter++;
        if(cleared == false && counter == ballCount){
          cleared = true;
          // here

          yield WaitForSecond(2.0);
          Application.LoadLevel("Title");
        }
      }
    }
  

Improve presentation

  • Change lighting
    • Edit>Render Settings
    • Click Ambient Light
    • Set Color and Brightness
  • Set ball Specular
    • Choose Specular in Shader in Inspector of Ball
    • Set Shininess
  • Set flicker effect on Trap
    • FlickerEffect.js
    private var originalColor : Color;
    
    function Start(){
      originalColor = renderer.material.color;
    }

    function Update(){
      var level : float = Mathf.Abs(Mathf.Sin(Time.time * 20));
      renderer.material.color = originalColor * level;
    }
  
  • Drag and drop FlickerEffect.js to Trap prefab


UnityJavaScriptGame Share Tweet +1