Lập trình game Zombie Hunter với Unity3D

Lập trình game Zombie Hunter với Unity3D

Xử lý menu game trong game Zombie Hunter với Unity3D Xử lý menu game trong game Zombie Hunter với Unity3D Xử lý menu game trong game Zombie Hunter với Unity3D Xử lý menu game trong game Zombie Hunter với Unity3D Xử lý menu game trong game Zombie Hunter với Unity3D 0/5 (15 reviews)

Xử lý menu game trong game Zombie Hunter với Unity3D

Đã đăng 2016-11-06 01:40:20 bởi HowKteam
0 bình luận 3351 lượt xem
Xử lý menu game trong game Zombie Hunter với Unity3D 0 /5 stars (0 reviews)
 

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 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;
    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;
	// Use this for initialization
	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.5f);
    }

	// 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;    
    public float shootTime = 0.5f;
    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;
	// Use this for initialization
	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, 2f);
    }

    void Attack()
    {
        if (Time.time >= lastAttackTime + attackTime)
        {            
            AttackAnim(true);
            UpdateAttackTime();
            player.GetComponent<PlayerController>().GetHit(damge);
        }
        else
        {
            AttackAnim(false);
        }
    }

	// Update is called once per frame
	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;
	// Use this for initialization
	void Start () {
        Time.timeScale = 1;
        menu.SetActive(false);
	}
	
	// Update is called once per frame
	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 game Demo

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ó

Chia sẻ:
Thảo luận Hỏi và đáp Báo lỗi bài viết
Hủy bỏ   hoặc  
Hủy bỏ   hoặc  
Hủy bỏ   hoặc  

Chiến dịch

Kteam - Howkteam Free Education