For week 6, the lecturer provided me with a useful tutorial from the DEV community to set up a basic game in Unity.
I ignored the player movement part, since that part was already done.
I followed the camera part of tutorial, and then googled various sites in order to adapt their script to 2D top-down. I eventually got the camera working with lots of testing.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraScript : MonoBehaviour
// Making a public variable for Player.
{
public GameObject playerDiamond;
private Vector3 offset;
// Start is called before the first frame update.
void Start()
{
offset = transform.position - playerDiamond.transform.position;
}
// Update is called once per frame.
void LateUpdate()
{
// Make Camera match player's transform position.
transform.position = playerDiamond.transform.position + offset;
}
}
Figure 1: Camera Script that follows the player from overhead.
I changed playercube to playerDiamond, since it’s actually a diamond in my game, not a cube like in the tutorial’s. I also wasn’t sure if Vector2 should be used instead, but I think with my experimenting, keeping Vector3 made the code simpler.
Figure 2: Camera tracking the player in my Unity project.
The next stage in the tutorial was concerned with creating a game over screen. There were 3 steps involved: creating “Game Over” text that filled the screen; deactivating all the game objects for that screen; and constructing a way to reload the game.
There were numerous problems at every stage, but gradually I found solutions to the problems that came up. I think the tutorial was also slightly wrong, since it assigned a variable true, checked if it was true, and then assigned it to be true again inside the check. I could not see any other script that interacted with it, so I changed the first assignment to be false and then the check to look for false. The theory behind this is that it won’t execute more than once, which could possibly cause errors or slow down the execution depending on what code executed following the success of the check.
The tutorial showed me to put all the code inside a Game Manager script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
bool gamehasEnded = false;
public GameObject Floor;
public GameObject GameOverPanel;
public GameObject Player;
public void GameOver()
{
if (!gamehasEnded)
{
gamehasEnded = true;
foreach (GameObject enemy in enemies)
{
enemy.SetActive(false);
}
Floor.SetActive(false);
GameOverPanel.SetActive(true);
}
}
void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Restart();
}
}
}
Figure 3: Game Manager Script.
I made the pressing down of the spacebar trigger restart, which just reloads the scene.

Figure 4: Game Over Screen in my Unity Project.
Next week, I’ll look at adding enemies and collision.