Không có gì tuyệt vời hơn là luyện tập với ví dụ thực tế. Nào cùng nhau thử thách bản thân với trò chơi nổi tiếng: Flappy bird.
Bạn nên có kiến thức về:
Lập trình C# cơ bản Class OOP trong C#
Code BackGroundMove.cs
using UnityEngine;
using System.Collections;
public class BirController : MonoBehaviour {
public float flyPower = 100 ;
public AudioClip flyClip;
public AudioClip gameOverClip;
private AudioSource audioSource;
GameObject obj;
GameObject gameController;
void Start () {
obj = gameObject;
audioSource = obj.GetComponent<AudioSource>();
audioSource.clip = flyClip;
if (gameController == null )
{
gameController = GameObject.FindGameObjectWithTag("GameController" );
}
}
void Update () {
if (Input.GetMouseButton(0 ))
{
audioSource.Play();
obj.GetComponent<Rigidbody2D>().AddForce(new Vector2(0 , flyPower));
}
}
void OnCollisionEnter2D(Collision2D other)
{
EndGame();
}
void EndGame()
{
audioSource.clip = gameOverClip;
audioSource.Play();
gameController.GetComponent<GameController>().EndGame();
}
}
Code BirdController.cs
using UnityEngine;
using System.Collections;
public class BirController : MonoBehaviour {
public float flyPower = 100 ;
public AudioClip flyClip;
public AudioClip gameOverClip;
private AudioSource audioSource;
GameObject obj;
GameObject gameController;
void Start () {
obj = gameObject;
audioSource = obj.GetComponent<AudioSource>();
audioSource.clip = flyClip;
if (gameController == null )
{
gameController = GameObject.FindGameObjectWithTag("GameController" );
}
}
void Update () {
if (Input.GetMouseButton(0 ))
{
audioSource.Play();
obj.GetComponent<Rigidbody2D>().AddForce(new Vector2(0 , flyPower));
}
}
void OnCollisionEnter2D(Collision2D other)
{
EndGame();
}
void EndGame()
{
audioSource.clip = gameOverClip;
audioSource.Play();
gameController.GetComponent<GameController>().EndGame();
}
}
Code WallMove.cs
using UnityEngine;
using System.Collections;
public class WallMove : MonoBehaviour {
public float moveSpeed;
public float minY;
public float maxY;
public float oldPosition;
private GameObject obj;
void Start()
{
obj = gameObject;
oldPosition = 10 ;
moveSpeed = 5 ;
minY = -1 ;
maxY = 1 ;
}
void Update()
{
obj.transform.Translate(new Vector3(-1 * Time.deltaTime * moveSpeed, 0 , 0 ));
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag.Equals("ResetWall" ))
obj.transform.position = new Vector3(oldPosition, Random.Range(minY, maxY + 1 ), 0 );
}
}
Code GameController.cs
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class GameController : MonoBehaviour {
bool isEndGame;
void Start () {
Time.timeScale = 0 ;
isEndGame = false ;
}
void Update () {
if (isEndGame)
{
if (Input.GetMouseButtonDown(0 ))
{
SceneManager.LoadScene(0 );
}
}
else
{
if (Input.GetMouseButtonDown(0 ))
{
Time.timeScale = 1 ;
}
}
}
public void EndGame ()
{
isEndGame = true ;
Time.timeScale = 0 ;
}
}
File Assets
Bài sau chúng ta sẽ cùng nhau tìm hiểu về UI làm menu.
Đừng quên: “Luyện tập – Thử thách – Không ngại khó ”
Tải xuống
Tài liệu
Nhằm phục vụ mục đích học tập Offline của cộng đồng, Kteam hỗ trợ tính năng lưu trữ nội dung bài học Giới thiệu audio - Âm thanh - trong lập trình game với Unity3D dưới dạng file PDF trong link bên dưới.
Ngoài ra, bạn cũng có thể tìm thấy các tài liệu được đóng góp từ cộng đồng ở mục TÀI LIỆU trên thư viện Howkteam.com
Đừng quên like và share để ủng hộ Kteam và tác giả nhé!
Project
Nếu việc thực hành theo hướng dẫn không diễn ra suôn sẻ như mong muốn. Bạn cũng có thể tải xuống PROJECT THAM KHẢO ở link bên dưới!
Thảo luận
Nếu bạn có bất kỳ khó khăn hay thắc mắc gì về khóa học, đừng ngần ngại đặt câu hỏi trong phần BÌNH LUẬN bên dưới hoặc trong mục HỎI & ĐÁP trên thư viện Howkteam.com để nhận được sự hỗ trợ từ cộng đồng.