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.
- Click the “Create” button in the “Hierarchy” view.
- Select “3D Object” -> “Cube”.
- Set the object name as “Danger Wall”.
- 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”.
- Click the “Create” button in the Project Browser.
- Select “Material” to create “DangerWallMaterial”.
- Set the color in the “Inspector” view. It is as follows. (Set “Albedo” to red color, Set “Emission” to “1.5” as red color)
- Drag-and-drop “DangerWallMaterial” onto “DangerWall” object in the “HIerarchy” view.
- 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…
- Put three walls at the scene.
- 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”.
- Click the “Create” button in the “Hierarchy” view.
- Select “Create Empty” to change the name of the created object to “DangerWalls”.
- Drag-and-drop all “DangerWall” object onto “DangerWalls”. It makes a parent-child relation.
- 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.
- Select “DirectionalLight” in the “Hierarchy” view.
- 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.
- Select “DangerWall” in the “Hierarchy” view.
- Click the “Add Component” button in the “Inspector” view.
- Select “New Script” to create the “DangerWall” component.
- Click “Apply” to reflect in all prefab.
3. Update script.
We set a call-back that receives “touched the wall” judgment.
- Open “DangerWall.cs” by “Monodeveop”.
- Change source code. It is as follows.
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.
- Open “DangerWall.cs” again by “Monodeveop”.
- Change source code. It is as follows.
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!!!
Author And Source
この問題について(ROLL-A-BALL TUTORIAL #8), 我々は、より多くの情報をここで見つけました https://qiita.com/amex/items/c36fe0e5263b77e72863著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .