📜 ⬆️ ⬇️

Play an hour or kill time in Unity

Hi, Habr!

Tonight I had free time and in the process of a useless journey across the expanses of my laptop I found an installed Unity3d, which my hands never reached.
Well, I decided something simple namut.

First, open Unity and create a project (3D).

Let's start?


Go to the menu GameObject> 3D Object> Cube and get a cube
')
image

Scale tool stretch our cube and reduce the height, in the end should get a plate

image

Add another 1 cube and give it the name player . Bind the camera to the "player"

image


Click Add Component> Physics> Rigidbody

image


In the window Rigidbody put a tick Use Gravity .

If you click on the Play button, we will see a falling cube on the plate

Create a maze


Create a new cube and make it like a wall

image

Duplicate the walls and create a maze out of them. Decorate the finish point with a bulging plate.
image

Add colors


image

It's time to code


Create a new C # script that we call playerActions

image

Open it.

Declare variables


public GameObject player; public int speed; public int rotationSpeed; 

Start () function


 player = (GameObject)this.gameObject; 

Update () function


The following lines will allow our "hero" to move back and forth.

 if (Input.GetKey (KeyCode.W)) { player.transform.position += transform.forward * speed * Time.deltaTime; } if (Input.GetKey (KeyCode.S)) { player.transform.position -= transform.forward * speed * Time.deltaTime; } 

To rotate the hero, use these lines.

 if (Input.GetKey (KeyCode.D)) { player.transform.Rotate (Vector3.up * rotationSpeed*Time.deltaTime); } if (Input.GetKey (KeyCode.A)) { player.transform.Rotate (Vector3.down * rotationSpeed*Time.deltaTime); } 

OnGUI () function


When we get to the finish line, we should say everything, go through the maze and get out of the game.

 if (player.transform.position.z >= 0.99 && player.transform.position.z <= 1.75 && player.transform.position.x <= -1.1 && transform.position.x>=-1.49) { if (GUI.Button (new Rect (200, 200, 800, 400), "You are win!\nExit")) {Application.Quit ();} ime.timeScale = 0; 

Well that's all


Almost everything is ready, it remains only to drag our script to the "hero"!
We start. We are checking. Nothing works?

That's right, we forgot to set the value to the variables speed and rotationSpeed !

Enter the values ​​and run

image


Add the audio track Game Object> Audio> Audio Source and bind to the player. In the field AudioClip we transfer music.

Save our scene.

Assembly


Go to the menu File> Build and Settings . Add our scene, choose the platform and collect.

Total


github

Source: https://habr.com/ru/post/347778/


All Articles