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 thú vị:
Zombie Hunter
Bạn nên có kiến thức về:
Lập trình C# cơ bản Class OOP trong C# Xem qua hai serial game 2D với Unity3D:
Flappy bird và Doge game
Code RotateGun.cs
using UnityEngine;
using System.Collections;
public class RotateGun : MonoBehaviour {
public Vector3 target;
void Start () {
}
void Update () {
LookAtCursor();
}
void LookAtCursor()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
target = hit.point;
}
transform.LookAt(target);
}
}
Code MoveToPlayer.cs
using UnityEngine;
using System.Collections;
public class MoveToPlayer : MonoBehaviour {
float moveSpeed;
public float minMoveSpeed = 0.05 f;
public float maxMoveSpeed = 0.3 f;
GameObject player;
GameObject lookAtTarget;
void Start () {
player = GameObject.FindGameObjectWithTag("Player" );
lookAtTarget = GameObject.FindGameObjectWithTag("LookTarget" );
UpdateMoveSpeed();
}
void UpdateMoveSpeed()
{
moveSpeed = Random.Range(minMoveSpeed, maxMoveSpeed);
}
void Move()
{
if (player == null || lookAtTarget == null )
return ;
transform.LookAt(lookAtTarget.transform.position);
transform.position = Vector3.Lerp(transform.position, player.transform.position, moveSpeed * Time.deltaTime);
}
void Update () {
Move();
}
}
Code SpawnEnemy.cs
using UnityEngine;
using System.Collections;
public class SpawnEnemy : MonoBehaviour {
GameObject[] spawnPoint;
public GameObject zombie;
public PlayerPrefs z;
public float minSpawnTime = 0.2 f;
public float maxSpawnTime = 1 ;
private float lastSpawnTime = 0 ;
private float spawnTime = 0 ;
void Start () {
spawnPoint = GameObject.FindGameObjectsWithTag("Respawn" );
UpdateSpawnTime();
}
void UpdateSpawnTime()
{
lastSpawnTime = Time.time;
spawnTime = Random.Range(minSpawnTime, maxSpawnTime);
}
void Spawn()
{
int point = Random.Range(0 , spawnPoint.Length);
Instantiate(zombie, spawnPoint[point].transform.position, Quaternion.identity);
UpdateSpawnTime();
}
void Update () {
if (Time.time >= lastSpawnTime + spawnTime)
{
Spawn();
}
}
}
Code PlayerController.cs
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public int damge = 1 ;
public float fireTime = 0.3 f;
public GameObject smoke;
public GameObject gunHead;
public float playerHealth = 10 ;
public AudioClip playerDeathSound;
private float playerCurrentHealth = 10 ;
private float lastFireTime = 0 ;
private Animator anim;
private AudioSource audioS;
private GameObject gameController;
void Start () {
anim = gameObject.GetComponent<Animator>();
UpdateFireTime();
audioS = gameObject.GetComponent<AudioSource>();
gameController = GameObject.FindGameObjectWithTag("GameController" );
}
void UpdateFireTime()
{
lastFireTime = Time.time;
}
void SetFireAnim(bool isFire)
{
anim.SetBool("isShoot" , isFire);
}
public void GetHit (float damge)
{
audioS.Play();
playerCurrentHealth -= damge;
if (playerCurrentHealth <= 0 )
{
Dead();
}
}
void Dead()
{
audioS.clip = playerDeathSound;
audioS.Play();
gameController.GetComponent<GameController>().EndGame();
}
void Fire()
{
if (Time.time >= lastFireTime + fireTime)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
#if UNITY_IOS || UNITY_ANDROID
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.tag.Equals("Zombie" ))
{
SetFireAnim(true );
InsSmoke();
hit.transform.gameObject.GetComponent<ZombieController>().GetHit(damge);
}
}
#else
RaycastHit hit;
if (Physics.Raycast(gunHead.transform.position, gunHead.transform.forward, out hit))
{
if (hit.transform.tag.Equals("Zombie" ))
{
SetFireAnim(true );
InsSmoke();
hit.transform.gameObject.GetComponent<ZombieController>().GetHit(damge);
}
}
#endif
UpdateFireTime();
}
else
{
SetFireAnim(false );
}
}
void InsSmoke()
{
GameObject sm = Instantiate(smoke, gunHead.transform.position, gunHead.transform.rotation) as GameObject;
Destroy(sm, 0.5 f);
}
void Update () {
if (Input.GetMouseButton(0 ))
{
Fire();
}
}
}
Code ZombieController.cs
using UnityEngine;
using System.Collections;
public class ZombieController : MonoBehaviour {
public int zombieHealth = 3 ;
public float shootTime = 0.5 f;
public bool isAttack = false ;
public float attackTime = 1 ;
public AudioClip zombieDeathSound;
public float damge = 1 ;
public bool IsShooten
{
get { return isShooten; }
set
{
isShooten = value ;
ShootenAnim(isShooten);
UpdateShootenTime();
}
}
private Animator anim;
private bool isShooten;
private bool isDead = false ;
private float lastAttackTime = 0 ;
private AudioSource audioS;
private float lastShootenTime = 0 ;
private GameObject player;
private GameObject gameController;
void Start () {
anim = gameObject.GetComponent<Animator>();
IsShooten = false ;
anim.SetBool("isDead" , false );
audioS = gameObject.GetComponent<AudioSource>();
player = GameObject.FindGameObjectWithTag("Player" );
gameController = GameObject.FindGameObjectWithTag("GameController" );
}
void UpdateShootenTime()
{
lastShootenTime = Time.time;
}
void UpdateAttackTime()
{
lastAttackTime = Time.time;
}
void ShootenAnim(bool isShooten)
{
anim.SetBool("isShooten" , isShooten);
}
void AttackAnim(bool isAttack)
{
anim.SetBool("isAttack" , isAttack);
}
public void GetHit (int damge)
{
if (isDead)
return ;
audioS.Play();
IsShooten = true ;
zombieHealth -= damge;
if (zombieHealth <= 0 )
{
Dead();
}
}
void Dead()
{
isDead = true ;
audioS.clip = zombieDeathSound;
audioS.Play();
anim.SetBool("isDead" , true );
gameController.GetComponent<GameController>().GetPoint(1 );
Destroy(gameObject, 2 f);
}
void Attack()
{
if (Time.time >= lastAttackTime + attackTime)
{
AttackAnim(true );
UpdateAttackTime();
player.GetComponent<PlayerController>().GetHit(damge);
}
else
{
AttackAnim(false );
}
}
void Update () {
if (IsShooten && Time.time >= lastShootenTime + shootTime)
{
IsShooten = false ;
}
if (isAttack)
{
Attack();
}
}
}
Code GameController.cs
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameController : MonoBehaviour {
public GameObject menu;
public Text txtPoint;
private int currentPoint = 0 ;
void Start () {
Time.timeScale = 1 ;
menu.SetActive(false );
}
void Update () {
}
public void GetPoint (int point)
{
currentPoint++;
txtPoint.text = "Zombie killed: " + currentPoint.ToString();
}
public void RestartGame ()
{
SceneManager.LoadScene(0 );
}
public void EndGame ()
{
menu.SetActive(true );
Time.timeScale = 0 ;
}
}
File Assets
project
Bài sau chúng ta sẽ cùng nhau tìm hiểu về xử lý thanh máu người chơi.
Đừ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 Xử lý menu game trong game Zombie Hunter 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.
Hay quá anh Long ơi! Mong Kteam sẽ có ra nhiều khóa thực chiến hướng dẫn làm game như vậy nũa!