ROLL-A-BALL TUTORIAL #8


A barrier and restart

We try to add a new rule. New rules are as follows.
The player becomes game over when the player touches the wall where the player should not touch.

The necessary elements are as bellow.
1. Create the wall.
2. Create an event when the player touched the wall.
3. Reload a scene when the player touched the wall.

1. Create the wall

We create a wall where player should not touch.

1-1. Create the Danger Wall Prefab.

  1. Click the “Create” button in the “Hierarchy” view.
  2. Select “3D Object” -> “Cube”.
  3. Set the object name as “Danger Wall”.
  4. Set “Danger Wall” object to “Prefab” folder.

1-2. Set the color of “Danger Wall”.

“Danger Wall” is dangerous. We want to make it look like danger.
Therefore we set the color of the wall as a “lighting wall”.

  1. Click the “Create” button in the Project Browser.
  2. Select “Material” to create “DangerWallMaterial”.
  3. Set the color in the “Inspector” view. It is as follows. (Set “Albedo” to red color, Set “Emission” to “1.5” as red color)

  1. Drag-and-drop “DangerWallMaterial” onto “DangerWall” object in the “HIerarchy” view.
  2. Click the “Apply” button to renew the “DangerWall” prefab.

1-3. Put “DangerWall”.

Put some “DangerWall” in the place where you want to put it.
But don’t put the wall on the item, because the player is not able to complete the game.

For example, put the wall like this…

  1. Put three walls at the scene.
  2. Put each wall. It is as follows.
  • DangerWall 1

position (x:4, y:0.5, z:3) rotation (x:0, y:0, z:0) scale(x:10, y:1, z:1)

  • DangerWall 2

position (x:4, y:0.5, z:-4) rotation (x:0, y:0, z:0) scale(x:10, y:1, z:1)

  • DangerWall 3

position (x:-5, y:0.5, z:4) rotation (x:0, y:90, z:0) scale(x:10, y:1, z:1)

1-4. Clean up “DangerWall” and change to static.

We clean up all “DangerWall” by using the “DangerWalls” object.
And “DangerWall” objects that does not move, therefore lock a place of the wall by using “static”.

  1. Click the “Create” button in the “Hierarchy” view.
  2. Select “Create Empty” to change the name of the created object to “DangerWalls”.
  3. Drag-and-drop all “DangerWall” object onto “DangerWalls”. It makes a parent-child relation.
  4. Check the box of “static” of “DangerWalls”.

1-5. Turn down the intensity of sunlight.

We turn down the intensity of sunlight. Because it is hard to see the wall.

  1. Select “DirectionalLight” in the “Hierarchy” view.
  2. Set the value of “Intensity” of the “Light” component in the “Inspector” view as “0.7”.


2. Set a judgment function that touched the wall.

We create the function that reboots the scene when the player touches the wall.

  1. Select “DangerWall” in the “Hierarchy” view.
  2. Click the “Add Component” button in the “Inspector” view.
  3. Select “New Script” to create the “DangerWall” component.
  4. Click “Apply” to reflect in all prefab.


3. Update script.

We set a call-back that receives “touched the wall” judgment.

  1. Open “DangerWall.cs” by “Monodeveop”.
  2. Change source code. It is as follows.
DangerWall.cs
using UnityEngine;
using System.Collections;

public class DangerWall : MonoBehaviour
{
    // call-back that called when player touched the object
    void OnCollisionEnter (Collision hit)
    {
        // program
    }
}

3-1. Reloading the scene.

Reloading the scene when the player touches the object that has a player tag.

  1. Open “DangerWall.cs” again by “Monodeveop”.
  2. Change source code. It is as follows.
DangerWall.cs
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class DangerWall : MonoBehaviour
{
    // call-back that called when player touched the object
    void OnCollisionEnter (Collision hit)
    {
        // when tag of object that touched the wall is “Player”.
        if (hit.gameObject.CompareTag ("Player")) {
            // get the number of current scene.
            int sceneIndex = SceneManager.GetActiveScene().buildIndex;
            // Reloading the current scene.
            SceneManager.LoadScene(sceneIndex);

    }
}

Try to play the game.

※ Unity5.3 may occur a bug that becomes dark when loading the current scene.
When occurring this trouble, try the following process.
1. Select “menu” -> “Window” -> “Lighting”.
2. Uncheck the checkbox of “auto”.
3. Click the “Build” button.

Done!!!