Search
• jollyyau

# Week 4 - Instantiating Ink Particles

My mentor suggested that I shadow some of my colleagues to learn more about Unity and software. Reflecting on today's experience, I realise that it is important to communicate with colleagues to bounce ideas, skills and learn from each other. By Learning to cold independently on a project allowed me to further understand how developers work with designers.

These are the notes I took today and will be useful in the future.

Step 1

The first things we need to establish is to make a list of GameObjects, which we can then define in the Inspector.

List<GameObject> prefabList = new List<GameObject>();

public GameObject ink;

public GameObject ink2;

public GameObject ink3;

public Transform container;

You can put as few or as many GameObjects as you want.

You must also make sure to establish WHERE you want to transform the GameObjects to.

Step 2

Attach your new script to an empty GameObject, and drag the prefabs into the slots:

We also need to set up the container so that it fills the entire canvas, and make it align to 0, 0 in bottom right corner.

Step 3

Let's go back to our script and add some lines to the update section.

Our logic is:If there is a touch input, make an object at that exact position.

Step 3 is letting the computer know where the coordinates are.

We need to make an "if" statement:

//Step 1: if there is touch count more than 0, begin the script

if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)

Alternatively, you can also swap touch with mouse input.

Since we are creating 2D objects, we just need to use Vector2 to make _pos variable equal to the Input.GetTouch(0).position

//Step 2: make the vector equal to the input touch position

Vector2 _pos = Input.GetTouch(0).position;

Debug.Log(_pos); - This just shows the console what the value "_pos" is, which is the touch coordinates

Step 4

In this step, we will start the object churning machine.

Firstly, we must add our objects to the list, and then put it through a randomizing engine.

//Step 3: Index the gameobjects in prefabList

int prefabIndex = UnityEngine.Random.Range(0, 3);

Then, we need to make them appear at the coordinates of the touch input.

We make the variable _go equal to the process of instantiating our previous prefab list.

• Instantiate(Object original, Transform parent);

• Other examples of instantiate

Then, we have to make _go transform equal to touch position, which we defined as _pos

//Step 4: make a variable equal to instantiating the prefab list

//Step 5: transform the instance into the vector that we set up earlier

var _go = Instantiate(prefabList[prefabIndex], container);

_go.transform.localPosition = _pos;

Step 5

We can now make some cool adjustments to _go, aka the instantiated object

• First, make the variable within a range.

• Then, transform it to _go

Examples:

• Angles: _go.transform.eulerAngles

• Scale: _go.transform.localScale

• Colours: _go.GetComponent<Image>().color

//Step 6: make a variable for random angles

//Step 7: transform the instance with new angles in variable

var _rot = Random.Range(0, 359);

_go.transform.eulerAngles = new Vector3(0, 0, _rot);

//Step 8: make a variable for random scales

//Step 9: transform the instance with new angles in variable

var _scaling = Random.Range(0.5f, 2);

_go.transform.localScale = new Vector3(_scaling, _scaling, _scaling);

//Step 10: make variables for random RGB values

//Step 11: transform the instance with new colors between 0 and 1 with Random.value

var _reed = Random.Range(0.2f, 0.7f);

var _gren = Random.Range(0.5f, 1f);

var _blue = Random.Range(0.5f, 1f);

_go.GetComponent<Image>().color = new Color(_reed, _gren, _blue, 1);

1 view

See All