About Unity

What is Unity?

Unity is a development environment for creating and designing computer games. With the help of the integrated programming environment MonoDevelop, you have the option of upgrading the numerous prefabricated Unity processes through individual programming in one of the following languages: UnityScript, C # (pronounced: C-Sharp) or Boo.

Unity projects can also be used for learning and training purposes. For example, projects from the areas of VR (Virtual Reality) or AR (Augmented Reality) can be involved.

Unity was first introduced in 2004. Unity is currently available in version 2017.2. Unity is offered in three different commercial versions, but also in the freely available Unity Personal version. These can be used privately or within a company if the profit is less than 100,000 US dollars per year.

What are we doing with Unity?

Unity offers a variety of possibilities. You should first deal with the most important elements that enable independent design and programming of the projects.

An introduction with simple surfaces and bodies provides an understanding of the elements in two-dimensional space and in three-dimensional space. Based on this, the games can be built step by step. You learn how the elements react to each other, especially under physical conditions.

You should get to know the elements of programming, for example with C #, from the ground up so that you can design versatile game sequences. This enables you to further change the existing games according to your own wishes and to develop your own games independently.

You should not simply combine prefabricated, complex elements, such as those offered in large numbers in the Unity Asset Store. Although these elements have a wealth of capabilities and offer numerous optical effects, the mere use and selective modification of these elements does little to understand their complex structure. They also do not simplify the understanding of the programmed course of the game. Many of the richly designed characters can only be created with external programs and must then first be imported into Unity.

It is recommended to develop two-dimensional games (2D games) first. You are already working with many Unity elements that will later also be required for the development of three-dimensional games (3D games). 2D games only take place at the level of the screen. They are clearer and easier to grasp than 3D games. So you can concentrate on learning how to develop games and don’t have to deal with orientation, rotation and perspective in three-dimensional space at the same time.

An example program

This section is an example of a program excerpt from a 2D game that you can develop yourself using Unity. It illustrates a typical game situation: The evaluation of collisions between game objects. If you already have some knowledge of a programming language, you will find many familiar elements in the code written in C # and in the short comments, but also elements that are specific to Unity and C #.

Of course, this demo example is not yet suitable for a thorough familiarization with Unity.

It’s a simple jump & run game, see Figure 1. The aim of the game is to collect points while avoiding dangers.

According to the instructions at the top of the playing field (see also Figure 2) you can move the black player in the appropriate directions with the arrow keys left and right . Use the up arrow key to make the player jump. You can also move the player left or right while jumping.

You can use the white platforms for a landing and for another jump. If the player touches the green win while jumping, you receive one point. Then the profit appears in a different place. As the game progresses, the profit becomes more difficult to achieve because it is placed higher up. After reaching six points, you have won the game. The time it took you to do this is recorded.

If the player touches one of the red, independently moving hazards, he loses one of his three lives. After losing your last life, the game is lost.

At the top left in Figure 1 you can see some displays, see also Figure 2:

  • Your current score,
  • the current number of lives you have left,
  • the current playing time since the player first moved and
  • the time it took you in the previous attempt

At the top right you will see two buttons:

  • to start a new game and
  • to terminate the entire application.

Various information appears at the top of the playing field. At the beginning there is the already mentioned short instructions. There is a corresponding comment after scoring a point, after losing a life and after the end of the game.

This is followed by the section of the program in which the collisions between the black player, a green win and a red risk are evaluated.

// Zusätzliche C#-Bibliotheken für Unity
using UnityEngine;
using UnityEngine.UI;

// Klasse, die das Verhalten des Spielers festlegt
public class Spieler : MonoBehaviour
{

// Funktion, die nach Eintreten einer Kollision durchlaufen wird
void OnCollisionEnter2D(Collision2D coll)
{
// Falls der schwarze Spieler mit einem grünen Gewinn kollidiert ist
if (coll.gameObject.tag == “Gewinn”)
{
// Punktzahl des Spielers erhöhen
anzahlPunkte = anzahlPunkte + 1;
// Grünen Gewinn kurzzeitig deaktivieren
gewinn.SetActive (false);

// Falls das Spiel noch nicht zu Ende ist
if (anzahlPunkte < 6)
{
// Anzeige der Punktzahl aktualisieren
punkteAnzeige.text = “Punkte: ” + anzahlPunkte;
if (anzahlPunkte == 1)
infoAnzeige.text = “Du hast bereits 1 Punkt!”;
else
infoAnzeige.text = “Du hast bereits ” + anzahlPunkte + ” Punkte!”;

// Grünen Gewinn nach zwei Sekunden wieder aktivieren
Invoke(“NaechsterGewinn”, 2);
}

// Falls das Spiel zu Ende ist
else
{
// Spiel-Ende anzeigen
infoAnzeige.text = “Du hast gewonnen!”;

// Startzustand für nächstes Spiel herstellen
spielGestartet = false;
gameObject.SetActive (false);

// Spielzeit speichern
PlayerPrefs.SetFloat(“zeitAlt”, Time.time – zeitStart);
PlayerPrefs.Save();
}
}

// Falls der schwarze Spieler mit einer roten Gefahr kollidiert ist
else if (coll.gameObject.tag == “Gefahr”)
{
// Anzahl der Leben des Spielers vermindern und anzeigen
anzahlLeben = anzahlLeben – 1;
lebenAnzeige.text = “Leben: ” + anzahlLeben;

// Schwarzen Spieler kurzzeitig deaktivieren
gameObject.SetActive (false);

// Falls noch weitere Leben übrig sind
if (anzahlLeben > 0)
{
// Anzeige der Anzahl der Leben aktualisieren
infoAnzeige.text = “Du hast nur noch ” + anzahlLeben + ” Leben!”;

// Schwarzen Spieler nach zwei Sekunden wieder aktivieren
Invoke (“NaechstesLeben”, 2);
}

// Falls das letzte Leben verloren wurde
else
{
// Spiel-Ende anzeigen
infoAnzeige.text = “Du hast verloren!”;

// Startzustand für nächstes Spiel herstellen
spielGestartet = false;
gewinn.SetActive (false);
}
}
}

// Grünen Gewinn an neuer Position wieder aktivieren
void NaechsterGewinn()
{
// Neue zufällige Position in x-Richtung ermitteln
float xNeu = Random.Range(-8.0f, 8.0f);

// Neue Position in y-Richtung ermitteln, abhängig vom Spielstand
float yNeu;
if (anzahlPunkte < 2) yNeu = -2.7f;
else if (anzahlPunkte < 4) yNeu = 0.15f;
else yNeu = 3;

// Grünen Gewinn auf die ermittelte Position setzen
gewinn.transform.position = new Vector3 (xNeu, yNeu, 0);

// Grünen Gewinn wieder aktivieren
gewinn.SetActive (true);
infoAnzeige.text = “”;
}

// Schwarzen Spieler an Startposition wieder aktivieren
void NaechstesLeben()
{
// Startposition festlegen
transform.position = new Vector3(0, -4.4f, 0);

// Schwarzen Spieler wieder aktivieren
gameObject.SetActive(true);
infoAnzeige.text = “”;
}

}

UnityScript