Lập Trình Game Zombie Hunter Với Unity3D - How Kteam
Có thể bạn quan tâm
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; // Use this for initialization void Start () { } // Update is called once per frame 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.05f; public float maxMoveSpeed = 0.3f; GameObject player; GameObject lookAtTarget; // Use this for initialization 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); } // Update is called once per frame 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.2f; public float maxSpawnTime = 1; private float lastSpawnTime = 0; private float spawnTime = 0; // Use this for initialization 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(); } // Update is called once per frame 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.3f; private float lastFireTime = 0; // Use this for initialization void Start () { UpdateFireTime(); } void UpdateFireTime() { lastFireTime = Time.time; } void Fire() { if (Time.time >= lastFireTime + fireTime) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { if (hit.transform.tag.Equals("Zombie")) { hit.transform.gameObject.GetComponent<ZombieController>().GetHit(damge); } } UpdateFireTime(); } } // Update is called once per frame void Update () { if (Input.GetMouseButton(0)) { Fire(); } } }Code ZombieController.cs
using UnityEngine; using System.Collections; public class ZombieController : MonoBehaviour { public int zombieHealth = 3; private Animator anim; private bool isShooten; public float shootTime = 0.5f; public bool isAttack = false; public float attackTime = 1; private float lastAttackTime = 0; public bool IsShooten { get { return isShooten; } set { isShooten = value; ShootenAnim(isShooten); UpdateShootenTime(); } } private float lastShootenTime = 0; // Use this for initialization void Start () { anim = gameObject.GetComponent<Animator>(); IsShooten = false; anim.SetBool("isDead", false); } 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) { IsShooten = true; zombieHealth -= damge; if (zombieHealth <= 0) { Dead(); } } void Dead() { anim.SetBool("isDead", true); Destroy(gameObject, 2f); } void Attack() { if (Time.time >= lastAttackTime + attackTime) { AttackAnim(true); UpdateAttackTime(); } else { AttackAnim(false); } } // Update is called once per frame void Update () { if (IsShooten && Time.time >= lastShootenTime + shootTime) { IsShooten = false; } if (isAttack) { Attack(); } } }File game Demo
File Assets
project
Bài sau chúng ta sẽ cùng nhau tìm hiểu về xử lý hiệu ứng bắn đạn.
Đừ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 Hiệu ứng Zombie tấn công người chơi 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.
CỘNG ĐỒNG HỎI ĐÁP HOWKTEAM.COM
GROUP THẢO LUẬN FACEBOOK Từ khóa » Trò Chơi Zombie Tấn Công
-
Game Zombie Tấn Công
-
Zombie Tan Cong - Game Vui
-
Trò Chơi Zombie Tấn Công! Online
-
Game Vui Nhộn ( Zombie Tấn Công ) - YouTube
-
Chơi Game Tấn Công Zombie - Attack Zombie - Mini, Game
-
Top 10 Game Zombie Miễn Phí Hay Nhất Cho điện Thoại Android, IOS
-
Game Zombie Tấn Công - Line98
-
Game Zombie Tấn Công
-
Đồ Chơi Game Hot Plants & Zombie 3 Hoa Quả Nổi Giận (ảnh Thật ...
-
Một Số Mẹo Chơi Plants Vs. Zombies 2 Có Thể Bạn Chưa Biết
-
Trò Chơi Zombie Tấn Công Miễn Phí Kẻ Thù Tấn Công - APK Pure
-
Game Vui Nhộn - Zombie Tấn Công - P3 - Bilibili
-
Lego Hoạt Hình | Trò Chơi ZOMBIE Tấn Công Batman Stop Motion