Create a game that looks like the below screen. Note the blue Player shape has a RigidBody, but is not effected by Gravity.
Add a script to the player so it moves like this:
Select the Camera and choose Align View to Selected to move the scene camera to the same position as the game camera. This will make it easier to make your level.
Below is the setup I used for the back of the level.
Below is the code I used to move the player. This uses all the same items from the previous game, except GetAxisRaw. This is explained below.
GetAxis add smoothing to make player movement feel more natural. GetAxisRaw will switch instantly between 0, 1 and -1. GetAxisRaw is better for reflex-based games as it accelerates faster.
Prefabs are premade objects that can be spawned into the level.
To turn your cubes into Prefabs:
Delete all but one of your cubes.
Drag the cube into your Project window.
Next, we need an object to spawn the blocks.
Right-click to create an Empty object and name this spawner.
Move this to where your cube is, ie. the same x, y and z position.
Delete the cube.
Add a SpawnerCtrl script.
Your script needs an Object of type GameObject called BlockPrefab.
In the Start method we instantiate, or create a copy of the BlockPrefab, at the same position as the spawner and rotated the same direction as the spawner.
This will spawn one block. What happens if we spawn blocks in the Update Method?
We can set up a new Method and call this regularly to spawn block regularly.
This is fine for 1 block, but we need set of 4.
Create 5 children objects called SpawnPoint.
Position these slightly below the Spawner and spread out.
We need to add the SpawnPoints to the SpawnerCtrl.
Add public GameObject called SpawnPoint.
Square Brackets, [ ], show this will hold a set of GameObjects.
Click on the Spawner to select it.
Use the padlock to lock this in the inspector.
Select all 5 Spawn Points.
Drag into the SpawnerCtrl.
Unlock the inspector.
Next, we need a loop to process each spawn point.
You should now get a set of blocks every second.
NOTE - I turned off the code to restart the level to test this.
We can use two lines of code to skip a block.
25 generates a random number that corresponds to one of the spawn points.
Line 28 is makes it so the block only spawns if it is not the random number.
You might want to adjust the size of the blocks and their colliders.
Double click the prefab to edit it.
Adjust the size of the box collider.
1, 1, 1 means it is the same size as the drawn shape.
0.8, 0.8, 0.8 means it is 80% of the size.
This code is in PlayerCtrl.
Next, we need some variables for the score. Call these PlayerScore and PlayerHighScore. These should both be floats.
This code is in PlayerCtrl.
In update increase the score by Time.deltaTime * 1000. Time.deltaTime is the time since the last frame.
During a second this will add to 1.
Multiplying by 1000 gives 1000 points per second.
Use Debug.Log to test this.
Next, we need a text area to display the score.
To add this select UI > Legacy > Text.
Note this has been made legacy since the last version of Unity I used, I'll look at the new version for next time.
Text objects appear inside the canvas.
The canvas is overlaid into the game world, but is scaled to fit into the camera so always scales differently to the other game objects.
Position the score text and use the Text Properties to resize this.
Add a using statement for UI at the top of the script.
using UnityEngine.UI;
Add a variable to PlayerCtrl to hold the text object.
Drag the text object into the script.
We can now write the score to the text object.
Add another text area to the canvas and add this into the code.
Add an if statement so PlayerHighScore changes if PlayerScore goes above it.
Write the High Score to the screen.
The High Score will reset when the level resets. Make it static to fix this.
Note, this will not save between sessions.
Add more text to the canvas to display a message for 'You Died!' or similar.
Add child text to inform the player how to restart.
Add space for the text in the code, we only need to worry about the main message as we can control the child by controlling the parent.
Add the Death Message to the Script in the Unity Editor.
Add an if else statement to the Update method so that the score and movement is possible when the player is alive. When the player is dead they can press the space button to restart.
Note the two highlighted rows are copied from the OnCollisionEnter() method.
When the player hits a Block they die and get the option to restart.
Copy line 63 (or your equivalent) and paste into your Start method.
Edit the line in your Start method so the DeathMessage is hidden.
When the game is running open the Stats panel and make a note of the Screen Size.
Open the Build Settings, add the Scene to your build settings and press the Player Settings button.
In the Player Settings add the width and height of the game.
Go back to Build settings and press build.
Create a folder for your build next to your project folder.
DO NOT BUILD INSIDE YOUR PROJECT FOLDER!
Wait for the game to Build