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.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/e2a76956-bf3b-84ef-c7ed-6dde7835a2c0.png)

##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)
 
![図2.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/0c8b372e-7bd5-2edc-7347-e3be5463de8c.png)

1. Drag-and-drop “DangerWallMaterial” onto “DangerWall” object in the “HIerarchy” view.
2. Click the “Apply” button to renew the “DangerWall” prefab.
 
![図3.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/2e9967ea-f448-eb10-9a12-402d7dc9664e.png)

##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…
 
![図4.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/5b37689f-be14-657a-8609-d985f8aad158.png)

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”.
 
![図5.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/35798990-1e03-a00a-736e-38d3b644eba1.png)

##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”.
 
![図6.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/81d46734-5dac-9a72-0600-977336e2a0e7.png)
 
#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.
 
![図7.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/d4e2a2da-b748-cfab-8a2f-5489801eb8d9.png)
 
#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!!!