2D Game Basics

Parameters

Parameters can be controlled in the editor as long as they are defined as public or serializable variables.

You can define both simple variables and lists (array)

public float speed;

public Sprite[] images;


[SerializeField]
private Text scoreText;
    
[SerializeField]
private GameObject[] enemyTypes;

Instantiating objects

Objects can be instantiated using the function

newobj = Instantiate(objTemplate) as ObjType;

//from pregab - prefab must be in Resources folder
newobj1 = Instantiate(Resources.Load("enemy"));

// Instantiate the projectile at the position and rotation of this transform
Rigidbody projectile;
Rigidbody clone;
clone = Instantiate(projectile, transform.position, transform.rotation);

enemyOrc = Instantiate(Orc) as Enemy;

Transforms

The transformations of an object can be accessed through the attribute `transform` (https://docs.unity3d.com/ScriptReference/Transform.html)

Vector3 objectPosition = GameObject.transform.position;

GameObject.transform.position = new Vector3(posX, posY, posZ);

transform.Translate(Vector3.up * Time.deltaTime, Space.World);

transform.Rotate(Vector3.up * Time.deltaTime, Space.World);

Random

To generate random values you can use the Random class

Random.Range(-10.0f, 10.0f)
Random.Range(0, 8);

Control of some objects or components

The display or hiding of an object in the scene can be done through the activation function.

myObject.SetActive(false); // hide
myObject.SetActive(true); // show

Similarly, the components of an object can be controlled

this.GetComponent<BoxCollider>().SetActive(false); // deactivate
this.GetComponent<SpriteRenderer>().sprite = image; //setting an image

myObject.GetComponent<Text>().text = '123' //setting a text

You can also access child or parent items

 childObject=parentObject.GetChild("child_name");

 //setarea unei componente a elementului copil
 parentObject.GetChild("child_name").GetComponent<SpriteRenderer>().sprite = image; 

Wait

If you want to introduce expectations when running a script / functions you can use custom keys. The corutines are used to start asynchronous functions, which can be extended on several frames, and in which you can put the wait (pause).

void Start()
{
    StartCoroutine(Example());
}

IEnumerator Example()
{
    print(Time.time);
    yield return new WaitForSeconds(5);
    print(Time.time);
}

Input Events

To be able to detect if an item has been pressed, the OnMouseDown function can be used

void OnMouseDown()
{        
      gameObject.SetActive(false);
}
    

Note! This function is active only if the user clicks on a UI element or a Collider

Tasks

Making a 2D memory game

  • Configure a 2D scene for running
  • Add to the scene an image that represents a board (table)
  • Add two images to the scene:
    • one that represents the hidden element
    • one that represents the displayed element
  • Script the displayed element so that the click will hide and the hidden one will be displayed
  • Collect 3 more images for the displayed items
  • Make a game controller (in the scene there must be pairs of elements - so there will be 4 pairs positioned randomly at each run)
    • dynamic change of the image depending on which to write the following
    • dynamic instantiation of a grid of 4 × 4 elements (the first is already instantiated)
    • random positioning of type elements for each element in the scene
    • maintaining the currently selected elements (max. 1 pair is selected)
    • the asynchronous run of the check whether the pair was selected correctly or not
  • maintain and display a score. The score increases when you discover two identical elements.
  • Add a restart game button
pjv/laboratoare/en/02.txt · Last modified: 2019/10/23 14:33 by alexandru.gradinaru
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0