Dev Diary #8 – Scripting (2)

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

Introduction

As there was no real scripting required from us from the 2nd project we had, this scripting post will be referring to the scripting work that I contributed to the project over the first couple of weeks in the project development. Recently, I’ve started to gain interest in UI programming. So I had decided to use most of this project trying to develop my C# skills in relation to UI.

The Scripts

PlayerInput

This scripts main function is to handle the actions of the keys pressed on the controller. The update method handled mostly the joystick movement for dial sector selection in the main menu and in-game. Though also used axis so the same controls could be used for UI navigation more simply. The dial selection method used here I feel was not the most efficient way of handling it, but scripting is not a strong suit of mine and this ended up working, so I rolled with it (for now).
As the creator of the pause menu, I figured that adding in the pause menu here was best idea, looking back after doing so, I regret it. It caused a couple of minor problems that we could work around but if I could do it again, the pause menu would have its own script that gets called from here.
Had entered a few comments, Trigger methods are the work of Brady, not me.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;



public class PlayerInput : MonoBehaviour
{
public string selectedShape;
public ParticleSystem laserFire;
private float horizontalDialSelection;
private float verticalDialSelection;
private int standardScore = 20;
private GameObject pauseMenu;
public static bool isPaused;

void Start()
{
pauseMenu = GameObject.Find ("PauseMenu");
pauseMenu.SetActive (false);
}

void Update()
{
horizontalDialSelection = Input.GetAxis("Horizontal");
verticalDialSelection = Input.GetAxis("Vertical");

//Dial selection is nested under isPaused bool to prevent the dial selection from occuring in the pause menu
if (isPaused == false)
{
if (horizontalDialSelection < 0)
{
selectedShape = "Square";
UIController.LeftDialSelect.SetActive(true);
UIController.RightDialSelect.SetActive(false);
UIController.UpperDialSelect.SetActive(false);
UIController.BottomDialSelect.SetActive(false);
}
else if (horizontalDialSelection > 0)
{
selectedShape = "Circle";
UIController.RightDialSelect.SetActive(true);
UIController.UpperDialSelect.SetActive(false);
UIController.LeftDialSelect.SetActive(false);
UIController.BottomDialSelect.SetActive(false);
}
else if (verticalDialSelection < 0)
{
selectedShape = "X";
UIController.BottomDialSelect.SetActive(true);
UIController.UpperDialSelect.SetActive(false);
UIController.LeftDialSelect.SetActive(false);
UIController.RightDialSelect.SetActive(false);
}
else if (verticalDialSelection > 0)
{
selectedShape = "Triangle";
UIController.UpperDialSelect.SetActive(true);
UIController.LeftDialSelect.SetActive(false);
UIController.RightDialSelect.SetActive(false);
UIController.BottomDialSelect.SetActive(false);
}
else
{
selectedShape = null;
UIController.LeftDialSelect.SetActive(false);
UIController.RightDialSelect.SetActive(false);
UIController.BottomDialSelect.SetActive(false);
UIController.UpperDialSelect.SetActive(false);
}

if (Input.GetButtonDown("Vapourise"))
{
laserFire.Play();
}
else
{
laserFire.Stop();
}
}



//If player hits the pause button, it activates the menu and pauses gameplay.
if (Input.GetButtonDown("Pause"))
{
pauseToggle ();
}
}

public void pauseToggle()
{
pauseMenu.SetActive(!pauseMenu.activeInHierarchy);
Debug.Log("Pause Button Hit");
if (pauseMenu.activeInHierarchy)
{
Time.timeScale = 0;
isPaused = true;
}
else
{
Time.timeScale = 1;
isPaused = false;
}
}

void OnTriggerExit2D(Collider2D collision)
{
collision.gameObject.GetComponent().gravityScale = 1;
collision.gameObject.GetComponent().enabled = false;
collision.gameObject.tag = "Shape";
}

void OnTriggerStay2D(Collider2D collision)
{

//if the shape passing the line is the selected shape and you press the vapourise button...
if (selectedShape == collision.gameObject.tag && Input.GetButtonDown("Vapourise"))
{
//destroy the shape
Destroy(collision.gameObject);

//NOTE: temporary score setting, replace with detecting perfects and goods
int score = standardScore;
//call the RewardScore function in the PlayerScore class to update the currentScore
PlayerScore.Instance.RewardScore(1, score);
}
}
}

SceneManager

This is the initial code used to load the UI scene in additively on top of the Game Scene. I had done this before but the previous method I am used to doing (using LoadLevelAdditive) is now obsolete. So I had to learn how the new Scene Management system works here. Didn’t take too long to get my head around so this was a fairly quick script overall.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneManagement : MonoBehaviour {

private string currentScene;

void Update ()
{

}

// Use this for initialization
void Start ()
{
currentScene = SceneManager.GetActiveScene ().name;
if (SceneManager.GetSceneByName ("GameScene").name == currentScene)
{
SceneManager.LoadScene("UI", LoadSceneMode.Additive);
}
}
}

UIController

This script is to handle the events and updates to the elements stored on the UI. I’m honestly still a little confused about when to and not to use static variables. People say it’s packs a punch on your games’ performance which I get, but its the simplest way I know to be able to access the object from another script, which is what static variables are used for. But I know after this I am going to need to read up on when and how to use these efficiently because I just get that feeling I’m not doing it correctly, even though the script still works.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIController : MonoBehaviour {

public static GameObject UpperDialSelect;
public static GameObject BottomDialSelect;
public static GameObject LeftDialSelect;
public static GameObject RightDialSelect;
public Text ScoreText;
public Text MultiplierText;

void Awake()
{
UpperDialSelect = GameObject.Find ("UpperSelect");
BottomDialSelect = GameObject.Find ("BottomSelect");
LeftDialSelect = GameObject.Find ("LeftSelect");
RightDialSelect = GameObject.Find ("RightSelect");
}

void Update()
{
ScoreText.text = "Score: " + PlayerScore.Instance.currentScore.ToString ();
MultiplierText.text = "Mulitplier: " + PlayerScore.Instance.currentMultiplier.ToString ();
}
}

Source control

Continued use of source control with a collaborator further proves a abidance with coding standards for a workplace. It made all of this a breeze when working on the same scripts at the same time. Getting few conflicts but resolving them was fairly straightforward.
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: