Dev Diary #3 – Scripting

//csharppad.com/embed.js Codepad.all();

Introduction

During project one I came to the conclusion that I really haven’t improved much on my scripting abilities since last trimester. It’s obvious that I need more experience with programming in order to get certain more advanced systems to work. I had gone into coding this project knowing what I wanted to do, but not necessarily how to do it so this is where I struggled, more on this in the Project 1 post-mortem blog.

The Scripts

PerspectiveSwitcher & MatrixBlender

After doing some research on how to get my desired effect, I found these scripts through some online forums made publicly available in response to a help forum post. The coding especially in matrixBlender was not something I had seen before nor did I realise this was what was needed to get the effect I was looking for. At least now I understand what to look for when needing help with similar problems.

GameController

This Script was used to control player spawn locations 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameController : MonoBehaviour {

public string exitTaken;
public GameObject player;
public GameObject spawnedPlayer;
public Transform playerSpawnPoint;

// Use this for initialization
void Start ()
{
ExitModifier.levelEnded = true;
}

// Update is called once per frame
void Update ()
{
//Determines where on the level the player will spawn
if (exitTaken == "RightExit")
{
playerSpawnPoint = GameObject.FindGameObjectWithTag ("SpawnLeft").transform;
Debug.Log (playerSpawnPoint.name);
if (ExitModifier.levelEnded == true)
{
spawnedPlayer = Instantiate (player, playerSpawnPoint);
}
}
if (exitTaken == "LeftExit")
{
playerSpawnPoint = GameObject.FindGameObjectWithTag ("SpawnRight").transform;
Debug.Log (playerSpawnPoint.name);
if (ExitModifier.levelEnded == true)
{
spawnedPlayer = Instantiate (player, playerSpawnPoint);
}
}
if (exitTaken == "UpperExit")
{
playerSpawnPoint = GameObject.FindGameObjectWithTag ("SpawnBottom").transform;
Debug.Log (playerSpawnPoint.name);
if (ExitModifier.levelEnded == true)
{
spawnedPlayer = Instantiate (player, playerSpawnPoint);
}
}
if (exitTaken == "BottomExit")
{
playerSpawnPoint = GameObject.FindGameObjectWithTag ("SpawnUpper").transform;
Debug.Log (playerSpawnPoint.name);
if (ExitModifier.levelEnded == true)
{
spawnedPlayer = Instantiate (player, playerSpawnPoint);
}
}
}
}

PlayerController

This was script was for some basic controls while testing the level loading, it was never finished.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {

public float moveSpeed = 10;
public GameObject maincamera;
public Rigidbody2D PlayerRB;

// Use this for initialization
void Start ()
{

}

// Update is called once per frame
void Update ()
{
//makes sure the player is always aligned to be facing the camera no matter what face of the cube the level is on.
transform.rotation = Quaternion.LookRotation (-maincamera.transform.forward, maincamera.transform.up);

if(Input.GetKey(KeyCode.W))
{
PlayerRB.transform.Translate (Vector3.up * Time.deltaTime, maincamera.transform);
}
if(Input.GetKey(KeyCode.S))
{
PlayerRB.transform.Translate (Vector3.down * Time.deltaTime, maincamera.transform);
}
if(Input.GetKey(KeyCode.A))
{
PlayerRB.transform.Translate (Vector3.left * Time.deltaTime, maincamera.transform);
}
if (Input.GetKey (KeyCode.D))
{
PlayerRB.transform.Translate (Vector3.right * Time.deltaTime, maincamera.transform);
}
//Controls below not yet set - current basic controls are just used for testing for now
// if (Input.GetKey (KeyCode.Space))
// {
// //Shoot ();
// }
// if (Input.GetKey (KeyCode.UpArrow))
// {
//
// }
// if (Input.GetKey (KeyCode.DownArrow))
// {
//
// }
// if (Input.GetKey (KeyCode.RightArrow))
// {
// PlayerRB.transform.Translate (Vector3.right * Time.deltaTime, maincamera.transform);
// }
// if (Input.GetKey (KeyCode.LeftArrow))
// {
// PlayerRB.transform.Translate (Vector3.right * Time.deltaTime, maincamera.transform);
// }
}
//Test detection for when the player should die.
void OnCollision (Collision2D other)
{
Debug.Log ("player dead");
}
}

LevelLoader

This is where majority of my scripting work went into in this project. It involved storing prefab levels in arrays and loading new levels from the correct array depending on the exit that was taken on a previous level. This is an obvious example that I need to do more research into different programming styles and creating systems that would me much more efficient. I have the feeling I could have achieved the same result as the code below within 100 lines if I actually knew what I was doing with it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class LevelLoader : MonoBehaviour {
// Use this for initialization


public GameObject[] LeftLevels;
public GameObject[] RightLevels;
public GameObject[] BottomLevels;
public GameObject[] UpperLevels;
public string exitTaken;
public GameObject currentAnchor;
public GameObject nextAnchor;
public GameObject currentLevel;
public GameObject nextLevel;
public Transform MainCamera;

void Awake ()
{
//Loads all level types into their respected arrays
LeftLevels = Resources.LoadAll ("LeftLevels") as GameObject [];
RightLevels = Resources.LoadAll ("RightLevels") as GameObject [];
BottomLevels = Resources.LoadAll ("BottomLevels") as GameObject [];
UpperLevels = Resources.LoadAll ("UpperLevels") as GameObject [];
}

void Start ()
{
ExitModifier.levelEnded = true;
exitTaken = "RightExit";
currentAnchor = GameObject.FindGameObjectWithTag ("Anchor1");
nextAnchor = GameObject.FindGameObjectWithTag ("Anchor1");
}

void Update ()
{
MainCamera = gameObject.transform;
NextLevel ();
}

//determines the correct anchor position for the next level to be loaded onto.
void NextLevel ()
{
if(currentAnchor.tag == "Anchor1" && ExitModifier.levelEnded == true)
{
Debug.Log("Load from Anchor1 Detected");
if (exitTaken == "RightExit")
{
nextAnchor = GameObject.FindGameObjectWithTag ("Anchor2");
LoadLeftLevel ();
}
if (exitTaken == "LeftExit")
{
nextAnchor = GameObject.FindGameObjectWithTag ("Anchor4");
LoadRightLevel ();
}
if (exitTaken == "UpperExit")
{
nextAnchor = GameObject.FindGameObjectWithTag ("Anchor5");
LoadBottomLevel ();
}
if (exitTaken == "LowerExit")
{
nextAnchor = GameObject.FindGameObjectWithTag ("Anchor6");
LoadUpperLevel ();
}
ExitModifier.levelEnded = false;
}

if(currentAnchor.tag == "Anchor2" && ExitModifier.levelEnded == true)
{
Debug.Log("Load from Anchor2 Detected");
if (exitTaken == "RightExit")
{
nextAnchor = GameObject.FindGameObjectWithTag ("Anchor3");
LoadLeftLevel ();
}
if (exitTaken == "LeftExit")
{
nextAnchor = GameObject.FindGameObjectWithTag ("Anchor1");
LoadRightLevel ();
}
if (exitTaken == "UpperExit")
{
nextAnchor = GameObject.FindGameObjectWithTag ("Anchor5");
LoadBottomLevel ();
}
if (exitTaken == "LowerExit")
{
nextAnchor = GameObject.FindGameObjectWithTag ("Anchor6");
LoadUpperLevel ();
}
ExitModifier.levelEnded = false;
}
if(currentAnchor.tag == "Anchor3" && ExitModifier.levelEnded == true)
{
Debug.Log("Load from Anchor3 Detected");
if (exitTaken == "RightExit")
{
nextAnchor = GameObject.FindGameObjectWithTag ("Anchor4");
LoadLeftLevel ();
}
if (exitTaken == "LeftExit")
{
nextAnchor = GameObject.FindGameObjectWithTag ("Anchor2");
LoadRightLevel ();
}
if (exitTaken == "UpperExit")
{
nextAnchor = GameObject.FindGameObjectWithTag ("Anchor5");
LoadBottomLevel ();
}
if (exitTaken == "LowerExit")
{
nextAnchor = GameObject.FindGameObjectWithTag ("Anchor6");
LoadUpperLevel ();
}
ExitModifier.levelEnded = false;
}
if(currentAnchor.tag == "Anchor4" && ExitModifier.levelEnded == true)
{
Debug.Log("Load from Anchor4 Detected");
if (exitTaken == "RightExit")
{
nextAnchor = GameObject.FindGameObjectWithTag ("Anchor1");
LoadLeftLevel ();
}
if (exitTaken == "LeftExit")
{
nextAnchor = GameObject.FindGameObjectWithTag ("Anchor3");
LoadRightLevel ();
}
if (exitTaken == "UpperExit")
{
nextAnchor = GameObject.FindGameObjectWithTag ("Anchor5");
LoadBottomLevel ();
}
if (exitTaken == "LowerExit")
{
nextAnchor = GameObject.FindGameObjectWithTag ("Anchor6");
LoadUpperLevel ();
}
ExitModifier.levelEnded = false;
}
if(currentAnchor.tag == "Anchor5" && ExitModifier.levelEnded == true)
{
Debug.Log("Load from Anchor5 Detected");
if (exitTaken == "RightExit")
{
nextAnchor = GameObject.FindGameObjectWithTag ("Anchor1");
LoadUpperLevel ();
}
if (exitTaken == "LeftExit")
{
nextAnchor = GameObject.FindGameObjectWithTag ("Anchor3");
LoadUpperLevel ();
}
if (exitTaken == "UpperExit")
{
nextAnchor = GameObject.FindGameObjectWithTag ("Anchor5");
LoadUpperLevel ();
}
if (exitTaken == "LowerExit")
{
nextAnchor = GameObject.FindGameObjectWithTag ("Anchor6");
LoadUpperLevel ();
}
ExitModifier.levelEnded = false;
}
if(currentAnchor.tag == "Anchor6" && ExitModifier.levelEnded == true)
{
Debug.Log("Load from Anchor6 Detected");
if (exitTaken == "RightExit")
{
nextAnchor = GameObject.FindGameObjectWithTag ("Anchor1");
LoadBottomLevel ();
}
if (exitTaken == "LeftExit")
{
nextAnchor = GameObject.FindGameObjectWithTag ("Anchor3");
LoadBottomLevel ();
}
if (exitTaken == "UpperExit")
{
nextAnchor = GameObject.FindGameObjectWithTag ("Anchor5");
LoadBottomLevel ();
}
if (exitTaken == "LowerExit")
{
nextAnchor = GameObject.FindGameObjectWithTag ("Anchor6");
LoadBottomLevel ();
}
ExitModifier.levelEnded = false;
}
}

//Instantiates the correct level onto the correct anchor point determined but the nextAnchor() method then destroys the previous level once camera has changed view to the new active level.
void LoadLeftLevel()
{
nextLevel = Instantiate (LeftLevels [Random.Range(0, LeftLevels.Length)], nextAnchor.transform.position, nextAnchor.transform.rotation) as GameObject;
MainCamera.LookAt (nextAnchor.transform.position);
Destroy(currentLevel, 2.0f);
currentLevel = nextLevel;
currentAnchor = nextAnchor;
}
void LoadRightLevel()
{
nextLevel = Instantiate (RightLevels [Random.Range(0, RightLevels.Length)], nextAnchor.transform.position, nextAnchor.transform.rotation) as GameObject;
MainCamera.LookAt (nextAnchor.transform.position);
Destroy(currentLevel, 2.0f);
currentLevel = nextLevel;
currentAnchor = nextAnchor;
}
void LoadUpperLevel()
{
nextLevel = Instantiate (UpperLevels [Random.Range(0, UpperLevels.Length)], nextAnchor.transform.position, nextAnchor.transform.rotation) as GameObject;
MainCamera.LookAt (nextAnchor.transform.position);
Destroy(currentLevel, 2.0f);
currentLevel = nextLevel;
currentAnchor = nextAnchor;
}
void LoadBottomLevel()
{
nextLevel = Instantiate (BottomLevels [Random.Range(0, BottomLevels.Length)], nextAnchor.transform.position, nextAnchor.transform.rotation) as GameObject;
MainCamera.LookAt (nextAnchor.transform.position);
Destroy(currentLevel, 2.0f);
currentLevel = nextLevel;
currentAnchor = nextAnchor;
}
}

ExitModifier

This is a small script I had made in order to attach to the exits of levels and is used to tell the level loader what array of levels to load the next level from.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class ExitModifier : MonoBehaviour
{
public static bool levelEnded = false;
public string exitLocation;
public LevelLoader levelLoader;
public GameController gameController;

// Use this for initialization
void Start ()
{
levelLoader = GetComponent ();
gameController = GetComponent ();
}

// Update is called once per frame
void Update ()
{

}

//detects player entering a level exit and stores which exit it was
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
Debug.Log ("Player finished level");
levelLoader.exitTaken = exitLocation;
gameController.exitTaken = exitLocation;
levelEnded = true;
}
}
}

Source Control

All this code needs to be controlled, so time to get that source control in! I know its a solo project but it’s always good habbit for me to use source control for all my projects. I understand the importance of it and love the fact its just another layer of backup that provides further security toward making sure I don’t lose any data.

Software used: 

  • Unity
  • MonoDevelop
  • GitHub

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: