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;
private Animator anim;
GameObject obj;
GameObject gameController;
void Start () {
obj = gameObject;
audioSource = obj.GetComponent<AudioSource>();
audioSource.clip = flyClip;
anim = obj.GetComponent<Animator>();
anim.SetFloat("flyPower" ,0 );
anim.SetBool("isDead" , false );
if (gameController == null )
{
gameController = GameObject.FindGameObjectWithTag("GameController" );
}
}
void Update () {
if (Input.GetMouseButton(0 ))
{
if (!gameController.GetComponent<GameController>().isEndGame)
audioSource.Play();
obj.GetComponent<Rigidbody2D>().AddForce(new Vector2(0 , flyPower));
}
anim.SetFloat("flyPower" , obj.GetComponent<Rigidbody2D>().velocity.y);
}
void OnCollisionEnter2D(Collision2D other)
{
EndGame();
}
void OnTriggerEnter2D(Collider2D other)
{
gameController.GetComponent<GameController>().GetPoint();
}
void EndGame()
{
anim.SetBool("isDead" , true );
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;
using UnityEngine.UI;
using System.Reflection.Emit;
public class GameController : MonoBehaviour {
public bool isEndGame;
bool isStartFirstTime = true ;
int gamePoint = 0 ;
public Text txtPoint;
public GameObject pnlEndGame;
public Text txtEndPoint;
public Button btnRestart;
public Sprite btnIdle;
public Sprite btnHover;
public Sprite btnClick;
void Start () {
Time.timeScale = 0 ;
isEndGame = false ;
isStartFirstTime = true ;
pnlEndGame.SetActive(false );
}
void Update () {
if (isEndGame)
{
if (Input.GetMouseButtonDown(0 ) && isStartFirstTime)
{
StartGame();
}
}
else
{
if (Input.GetMouseButtonDown(0 ))
{
Time.timeScale = 1 ;
}
}
}
public void RestartButtonClick ()
{
btnRestart.GetComponent<Image>().sprite = btnClick;
}
public void RestartButtonHover ()
{
btnRestart.GetComponent<Image>().sprite = btnHover;
}
public void RestartButtonIdle ()
{
btnRestart.GetComponent<Image>().sprite = btnIdle;
}
public void GetPoint ()
{
gamePoint++;
txtPoint.text = "Point: " + gamePoint.ToString();
}
void StartGame()
{
SceneManager.LoadScene(0 );
}
public void Restart ()
{
StartGame();
}
public void EndGame ()
{
isEndGame = true ;
isStartFirstTime = false ; ;
Time.timeScale = 0 ;
pnlEndGame.SetActive(true );
txtEndPoint.text = "Your point\n" + gamePoint.ToString();
}
}
File Assets
Bài sau chúng ta sẽ cùng nhau tìm hiểu về cách biuld game ra đa nền tảng.
Đừ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 Animation 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.
với cả nó hiện cảnh báo : cannot preview transition need an animator controller nữa ạ, mong anh giải đáp giúp em, em cảm ơn
cho em hỏi một chút là cái animation của em ko chuyển sang được hình lúc con chim bị chết mà chỉ có cụp cánh và cất cánh thôi ạ, em có mò chỉnh transition duration thì lúc hiện được lúc không, không biết cái này là lỗi gì ạ