Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika...

31
Pertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta Tri Anggraeni, S.Kom., M.Sc. Video 5: 7.30 – 7.38

Transcript of Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika...

Page 1: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

Pertemuan 4Roll a Ball (Part 3)

Mata Kuliah Logika GameProgram Studi Teknologi PermainanSekolah Tinggi Multi Media YogyakartaTri Anggraeni, S.Kom., M.Sc.

Video 5: 7.30 – 7.38

Page 2: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

Alternatif posisi collectible objects

Page 3: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

Memberi warna pada collectible object (cube)

• Duplicate Background di folder Materials.

• Rename jadi Pick Up.

Page 4: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

• Ubah warnanya jadi kuning/sesuai keinginan.

• Drag material Pick Up ke prefab Pick up hingga semua cube berwarna kuning.

Klik

Page 5: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

“Memakan” objek Pick Up ketika Player sphere menabrak cube

Perlu mendeteksi collision (tabrakan) antara Player sphere & Pick up cube.

Perlu memiliki collision trigger new behavior.Perlu mengetes collision untuk memastikan objek

yang diambil benar.

• Objek Player, Pick Up, Ground, & Wall memiliki yang menginformasikan tentang collision.

• Jika collision tidak dites untuk mengetahui object mana yang ditabrak, objek yang salah bisa terambil.

Page 6: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

• Klik objek Player. Di Inspector: centang agar terlihat di Ground.

• Buka skrip PlayerController.

• Tampil di browser:

Klik

Klik

Page 7: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

Perlu mendeteksi & mengetes collision• Gunakan:

Klik Bisa digunakan untuk mendeteksi kontak antara objek Player & Pick up tanpa benar2 membuat physical collisionCopy ke PlayerController

Page 8: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

void OnTriggerEnter(Collider other) { Destroy(other.gameObject); }

Dipanggil Unity ketika objek Player menyentuh trigger colider.

void OnTriggerEnter(Collider other) {

Memberikan jalan untuk memperoleh suatu “lubang” di collider yang tersentuh.

Referensi ke trigger collider yang disentuh

Ketika menyentuh trigger collider objek lain: objek yang trigger collider-nya tersentuh itu di-destroy dengan Destroy(other.gameObject);

Page 9: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

• Menghapus game objek dari scene.

• Di Game Roll a Ball ini: objek yang trigger collider-nya tersentuh tidak di-destroy tapi di-deactivate (seperti men-deactivate objek Player di pertemuan sebelumnya).

Destroy(other.gameObject);

Page 10: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

• Ctrl ‘ sintaks gameObject jika ada koneksi internet atau ketikkan gameObject di kotak Search scripting di Unity Documentation yang telah tampil di browser tanpa koneksi internet. Tampil:

Klik

Page 11: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

Cari variable/function:

CompareTagMembandingkan tag objek2 game ke suatu nilai

string.

Copy ke PlayerController

(Label obj gm)

Page 12: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

SetActive

Untuk meng-activate/men-deactivate game object.

Kode programnya: gameObject.SetActive(false);

Copy ke PlayerController

Page 13: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

Then in PlayerController script now

• Slide 9: Di Game Roll a Ball ini: Objek yang trigger collider-nya tersentuh tidak di-destroy dengan Destroy(other.gameObject); tapi di-deactivate.

• Maka: hapus Destroy(other.gameObject); dari skrip PlayerController.

Page 14: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

• Karena skrip ini di objek Player, maka:if (other.gameObject.CompareTag("Player"))Harus diubah menjadi:if (other.gameObject.CompareTag("Pick Up"))

Page 15: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

• Karena skrip ini di objek Player & ketika bersentuhan: cube di-deactivate, maka:gameObject.SetActive (false); harus ditambahi otheragar objek other (Pick Up) yang di-deactivate (hilang).

Jika other tidak ditambahkan: ketika Player menyentuh Pick Up, Player yang menghilang.

Page 16: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

Cek Prefab Pick Up• Klik Prefab Pick Up. Cek tag-tag yang ada di

Inspector:

Klik

Belum ada tag Pick Up

Klik

Klik

Page 17: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

Case sensitive Harus sama dengan yang di skrip

Klik Prefab Pick Up.

Klik

Klik

Page 18: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

Klik Prefab Pick Up. Di Inspector: Box Collider: centang Is Trigger.

Save. Klik Play . Jalankan sphere Player & gerakkan ke cube.

Page 19: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

Unity mengoptimalkan fisika

Unity mengoptimalkan performance dengan cara menghitung semua volume static collider di scene & menyimpannya.Static collider tidak bergerak.Ketika cube digerakkan, dirotasikan, atau diubah

ukurannya, Unity menghitung static collider take resources.

Agar tidak begitu: set collider menjadi dynamic sebelum digerakkan.

Caranya: tambahkan komponen Rigidbody.Collider yang punya Rigidbody disebut dynamic.Collider yang tidak punya Rigidbody disebut static.

Page 20: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

Klik objek Pick Up. Di Inspector: klik Add Component, Rigidbody. Jalankan.Semua cube jatuh.Karena Use Gravity-nya tercentang.

Kalau Use Gravity-nya tidak dicentang: cube-nya sudah bisa tidak jatuh.

Cube sudah tidak merespon gravity tapi masih merespon Physics forces.

Physics forces: fisika yang bisa membuat gerakan berpindah tempat seperti bola menggelinding.

Page 21: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

Cube tidak perlu Physics forces

Cara menghilangkan Physics forces:Tetap centang Use Gravity.Centang Is Kinematic-nya agar Rigidbody-nya

menjadi kinematic.(Kinematic Rigidbody tidak bereaksi pada forces (daya) & dapat dianimasikan & digerakkan dengan transform).

Bagus diterapkan pada semua objek dengan collider/ trigger.

Standard rigidbody move using Physics forces.Kinematic rigidbody move using transform.

Page 22: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

Menyimpan jumlah cube yang ditangkap

• Di PlayerController: tambahkan: private int count;

• Di function Start: tambahkan: count = 0;

• “Mengambil cube” dilakukan di function OnTriggerEnter ketika other gameObject punya tag “Pick Up”.Tambahkan setelah baris SetActive (false);

count = count + 1;

Page 23: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri
Page 24: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

Menampilkan jumlah cube yang ditangkap

• Di Hierarchy Unity: Klik

Rename jadi Count Text

Page 25: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

Jika text tidak terlihat & ingin melihat:Klik2 pada Count Text, Ground, Gizmo, & geser2 scenesampai text terlihat

• Ubah warna text: di Inspector• Misal menjadi putih.• Ubah text menjadi:

Klik

Page 26: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

Tekan keyboard Shift & Alt, klik top left

Cek di Play mode:

Klik

Page 27: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

Di skrip PlayerController:

• Tambahkan: using UnityEngine.UI; public Text countText;

Untuk menyimpan reference ke UI text component di objek text.

• Set nilai awal countText di function Start:countText.text = "Count: " + count.ToString ();

Page 28: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

• Update text tiap mengambil cube:Di function OnTriggerEnter, tambahkan:countText.text = "Count: " + count.ToString ();

Menuliskan baris program yang sama di 1 skrip: hindari

Page 29: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

Solusi:• Buat function baru untuk menampung baris program

yang sama:

• Di function Start & OnTriggerEnter: panggil function SetCountText.

Page 30: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

Di Unity: klik objek Player

• Drag objek Count Text ke

• Sehingga:

• Save. Klik Play . Jalankan sphere Player & gerakkan ke cube.

Page 31: Pertemuan 4 Roll a Ball (Part 3) - WordPress.comPertemuan 4 Roll a Ball (Part 3) Mata Kuliah Logika Game Program Studi Teknologi Permainan Sekolah Tinggi Multi Media Yogyakarta. Tri

PR• Semua peserta kuliah:

1. Melanjutkan Game Roll a Ball berdasarkan video yang saya upload di sharedthings.wordpress.com.

2. Mencoba membuat Game Runner berdasarkan video yang saya upload di sharedthings.wordpress.com.

• Individu (gantian): Membuat tutorial tertulis dalam Bahasa Indonesia semacam ppt materi pertemuan ini. Boleh di ppt/ms word.- Robbi: Game Roll a Ball video terakhir & Game Runner (5

menit pertama)- Farhan, Arvinza: Game Runner (@10menit lanjutan)