Wednesday, May 31, 2017

Roll Ball Unity 5.6 Part 7 - Restart & Exit Game - Final Part

Part 6 bisa dilihat disini...
Di part 7 pengen pake bahasa English biar sekalian latian ehehe, jdi tar pas divideoin udh tau apa yang mau diomongin... B-)

Ok.......
In this part we'll gonna add menu when player win the game. This menu will be shown up after player has hit at least 13 balls.
So... First, let's add a button, hit Gameobject menu then choose UI, then button as shown in the picture below :
Choose 2D view, and adjust our view for button like this...

Then drag button to top right..

Aaaand.... Rename button to "Play Again"...
Duplicate above button and then give a name to "Exit"

Neext... rename canvas to "Win Menu" and button to restart and exit button...
Okie... It's time to play with script, let's open SphereController.cs. In that script we'll gonna hide WinMenu and show it again when user has hit at leat 13 balls. So add this line code below...
If you don't really understand above code, please leave a comment... :)
Track change on github can be seen here....

Neext... Let's add listener for those two buttons, so when hitting "Play Again", the game will be restarted and when hitting "Exit", game will exit.... those two listeners will be added also on SphereController.cs as shown below
Okiee.... That's all for this part.... Here's the demo..

Roll Ball Unity 5.6 Part 6 - Display Text, Play Fireworks & Sound

Part 5 bisa dilihat dilink ini...

Di part kelima ini kita akan nambain text dilayar yg isinya jumlah cube yg udah dihit dan text buat nandain klu udah menang... :)
So pertama-tama tambain empty_object buat nyimpent guitextnya. Terus ubah nama empty gameobjectnya jadi Display Text kek gini :

Terus buat empty gameobject lagi terus namain jadi hit Text kek gini :
Ters reset posisi Hit Text ke origin point :

Teruuus... tambain GUI Text ke Hit Textnya...
Teruuuss... nambain "Point: " di kolom Text dan ubah posisinya sesuai selera

Terus...... Hit Text-nya dijadiin child dari Display Text (drag and drop kek dipostingan sblmnya)..
Trs Hit Text diduplicate dan namanya diganti jadi Win Text kek gini :
Yang Win Text ini propertinya kyk gini :
Siiipsss... tampilannya udh siap...berikutnya adalaaah... scriptnya... so silahkan buka SphereController ters tambain ginian...
Untuk ngeliad perubahan yg lebih jelas, silahkan buka reponya di github disinih....
Eeeeh... ada yg luphaa..... lupa masukin fireworks pas menang... :v
Soo...tambain lagi kode dibagian showText() ahahaha.... kek gini ajjah...

Github commit...
mmmm....kekna gini ajjah... klu g' ngerti kodenya silahkan ditanyakan...ehehe... :D
Teruuuss... setelah fireworksnya udah ada...berikutnya adalah  suaranya.... So mari kita buat 1 biji folder buat nyimpen suara...terus drag and drop file mp3 ke dalam foldernya kek gini :
Terus tambain 1 biji gameobject audio source dan masukin fireworks2 ke audio source ini
Siiipsss.... berikutnya trigger soundsnya pas udh menang pake script (tutorial buat ngejalanin suara bisa dilihat disini...) . So tambain kode ini...
Perubahan kode digthub bisa diliat disini.... Klu g' ngerti kodenya silahkan ditanyakan... :)
Siiipsss.... Gitu ajjah buat yg part 6.... :)
Demonya seperti ini....

Part 7 bisa dilihat disni.....

Tuesday, May 30, 2017

Finding UI Button from Canvas Gameobject in Unity 5.6

So in this scene, I have canvas with two buttons as shown in the picture below
This restart button has a tag 'restart_tag' and exit with 'exit_tag' button...

And in this scene, I want to reference to my buttons using variabel in my script and this variabel is a private var not a public, so I can't drag and drop button because it's a private. So what we can do for achieving this ?
We have several ways... :)
First... We can use a public function from canvas class namely GetComponentsInChildren<Button>(), this function will gonna return all of the button within this canvas as an array. Here is the example code :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class canvas_sc1 : MonoBehaviour {

	private Button[] buttons;
	private Canvas cvs;
	// Use this for initialization
	void Start () {
		cvs = GetComponent<Canvas>();
		buttons = cvs.GetComponentsInChildren<Button> ();
		Debug.Log (buttons[0].tag);
		Debug.Log (buttons[1].tag);
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

When we run the game, we'll see that button[0] contains button for restart and another for exit
Another function that we can use is GetComponents. Here is the example :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class canvas_sc1 : MonoBehaviour {

	private Button[] buttons;
	private Button button;
	private Canvas cvs;
	// Use this for initialization
	void Start () {
		cvs = GetComponent<Canvas>();
		buttons = cvs.GetComponents<Button>();
		Debug.Log (buttons);
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}
but unfortunately, for unity 5.6, GetComponents doesn't work... :v
So we can just use GetComponentsInChildrend ... :)

Video :

Friday, May 26, 2017

Roll Ball Unity 5.6 Part 5

Part IV bisa dilhat disini....
Okie... di part 5 ini pertama-tama kita tambain tag ke prefabnya biar tar pas ketabrak sama bolanya bisa dideteksi... So select prefabnya, terus disudut kanan atas add new tag kek gini :




Naaah... klu prefabnya diset Tagnya ke hisoka_target, makaaaaa...semua target bakalan punya tag hisoka_target juga...
That's the power of prefabs... :D
Siiipsss... abis nambain tag, berikutnya kita tambain rigidbody ke prefabsnya biar sesudah di hit sama bolanya, cachenya bisa ilang atau g' direcalculate ulang meshnya sama unity, ini buat penghematan memory... :)
Abs ngeadd rigidbody, ganti propertinya jadi kek gini :
Ooowh..terus centang 'Is Trigger' di properti box collidernya biar tar bisa masuk ke method OnTriggerEnter :
Okiee... Next kita import partikel package....
Terus setelah di import, masuk ke folder Prefabs ParticelSystems
Naaah... disitu kita bisa milih2 banyak object prefabs.... daaaan... disini Hisoka milih yang Flare sm Fireworks ajjah... :)
So buat folder baru, namanya "Resources" (ini namanya harus sama dan pake Kapital R)
Naaah... drag and drop fireworks & flare ke folder Resources ini ....
Siiipsss.... naaah.... waktunya main script lagi... :D
Silahkan buka script buat ShereController yang udah dibuat dari part2 sebelumnya, terus tambain method OnTriggerEnter kyk gini :

Method onTriggerEnter itu ke trigger pas bolanya ngehit kubusnya. Terus pas bola ngehit cube, dicek apa tagnya sama dengan hisoka_target, klu sama tar kode dari line 30 smapai 37 dieksekusi yng intinya ngejalanin Flare terus ngedestroy(7 detik setelah hit, kode line 35) dan cube dihide pake method SetActive(false);
Demonya kyk gini... :
Seeeepssss... sekian dl untuk part V... :)


Part 6 bisa dilihat di link ini....

Roll Ball Unity 5.6 Part 4 - Iseng

Part 3 bisa dilihat dilink ini...

Naaah... dipart iv ini kita nambain dinding ke planenya biar bolanya g' jatuh ahaha... :D
So... Pertama-tama kita bikin empty game object buat ngekolompokin wallsnya....
Terus direname jadi walls..
Terus tambain cube...
Terus cubenya diganti namanya jadi rightWalls dan posisinya di reset ke origin poin (0,0,0)
Teruuuss... cubenya discale sesuai ukuran planenya, klu punya sy kek gini ukurannya....
Terusss... jadiiin rightWall sebagai child dari walls kek gini :
Yooosh... berikutnya kita tinggal duplicate rightWall, terus aturs posisinya, ters jadiin sebagai leftWall kekgini :
Terus front and backWall... :D

Siiipsss... Dindingnya udah bereesss.... berikutnya ituuu....... kubus yg muter2... :D
So pertama-tama tambain empty_object lagi, terus ubah namanya jadi targets, trs tambain cube, trs ubah namanya jadi target, terus cubenya di rotasi kek gini :
Berikutnya kita bikin kubusnya berotasi... Jadi silahkan tambain new script buat kubus ini, trs masukin kodenya kek gini :
Naaah.... Tar kubusnya muter2 kek gini... (itu nilai2 vector3 silahkan diganti2 sesuai selera)
Naah.... tar kubus2 yg jadi target ini kita duplikasi buat di hit sm bolanya,, so... karena banyak yg mau diduplikasi, tar klu ngubah2 sst bisa jdi repot, makanya bikin 1 biji Prefabs dari target kubus ini biar klu pengen ngubah sesuatu, cukup ngubah prefabsnya ajjah terus kubus2 yang lain jadi pada kena efek perubahannya... So withoud further ado, let's create a folder called Prefabs and then drag and drop our target to that folder like this :
Siiipss... berikutnya silahkan duplikasi kubusnya banyak2 sesuai selera.... :D, Klu punya sy kek gini ...

Klu gamenya di run... hasilnya kek gini :
Sippss... sekian dl untuk part yg ke IV yeeeei.... :)


Ke Part 5 bisa lewat link ini.....