Unity3D 坦克大戰(zhàn) 復盤 2021-03-11

Player.cs

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Player : MonoBehaviour {

? ? //屬性值

? ? public float moveSpeed=3;//移動速度

? ? private Vector3 bullectEulerAngles;

? ? private float timeVal;//攻擊CD

? ? private float defendTimeVal=3;//無敵時間3秒

? ? private bool isDefended=true;//無敵開啟

? ? //引用

? ? private SpriteRenderer sr;//精靈渲染

? ? public Sprite[] tankSprite;//上 右 下 左

? ? public GameObject bullectPrefab;//子彈預制件

? ? public GameObject explosionPrefab;//爆炸預制件

? ? public GameObject defendEffectPrefab;//無敵預制件

? ? public AudioSource moveAudio;//播放音頻組件

? ? public AudioClip[] tankAudio;//接受音頻

? ? private void Awake()//自動調用

? ? {

? ? ? ? //獲取組件<SpriteRenderer>

? ? ? ? sr = GetComponent<SpriteRenderer>();

? ? }

? ? // Use this for initialization

? ? void Start () {

}

//更新

void Update () {

? ? ? ? //是否處于無敵狀態(tài)

? ? ? ? if (isDefended)

? ? ? ? {

? ? ? ? ? ? //setactive是控制GameObject對象顯示/關閉

? ? ? ? ? ? defendEffectPrefab.SetActive(true);

? ? ? ? ? ? defendTimeVal -= Time.deltaTime;

? ? ? ? ? ? if (defendTimeVal<=0)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? isDefended = false;

? ? ? ? ? ? ? ? defendEffectPrefab.SetActive(false);

? ? ? ? ? ? }

? ? ? ? }



? ? }

? ? //固定更新

? ? private void FixedUpdate()

? ? {

? ? ? ? if (PlayerManager.Instance.isDefeat)

? ? ? ? {

? ? ? ? ? ? return;

? ? ? ? }

? ? ? ? Move();

? ? ? ? //攻擊的CD

? ? ? ? if (timeVal >= 0.4f)

? ? ? ? {

? ? ? ? ? ? Attack();

? ? ? ? }

? ? ? ? else

? ? ? ? {

? ? ? ? ? ? timeVal += Time.fixedDeltaTime;

? ? ? ? }

? ? }

? ? //坦克的攻擊方法

? ? private void Attack()

? ? {

? ? ? ? if (Input.GetKeyDown(KeyCode.Space))

? ? ? ? {

? ? ? ? ? ? //子彈產生的角度:當前坦克的角度+子彈應該旋轉的角度。

? ? ? ? ? ? Instantiate(bullectPrefab, transform.position,Quaternion.Euler(transform.eulerAngles+bullectEulerAngles));

? ? ? ? ? ? timeVal = 0;

? ? ? ? }

? ? }

? ? //坦克的移動方法

? ? private void Move()

? ? {

? ? ? ? //Vertical 鍵盤按上或下鍵時觸發(fā)

? ? ? ? float v = Input.GetAxisRaw("Vertical");

? ? ? ? //使小球按照y軸正方向以每幀*3的單位長度移動

? ? ? ? transform.Translate(Vector3.up * v * moveSpeed * Time.fixedDeltaTime, Space.World);

? ? ? ? if (v < 0)

? ? ? ? {

? ? ? ? ? ? //下

? ? ? ? ? ? sr.sprite = tankSprite[2];

? ? ? ? ? ? bullectEulerAngles = new Vector3(0, 0, -180);

? ? ? ? }

? ? ? ? else if (v > 0)

? ? ? ? {

? ? ? ? ? ? //上

? ? ? ? ? ? sr.sprite = tankSprite[0];

? ? ? ? ? ? bullectEulerAngles = new Vector3(0, 0, 0);

? ? ? ? }

? ? ? ? //如果上下動了

? ? ? ? if (Mathf.Abs(v)>0.05f)

? ? ? ? {

? ? ? ? ? ? //播放tankAudio[1]

? ? ? ? ? ? moveAudio.clip = tankAudio[1];


? ? ? ? ? ? if (!moveAudio.isPlaying)//如果不在播放

? ? ? ? ? ? {

? ? ? ? ? ? ? ? moveAudio.Play();//就播放

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? if (v != 0)

? ? ? ? {

? ? ? ? ? ? return;

? ? ? ? }


? ? ? ? float h = Input.GetAxisRaw("Horizontal");

? ? ? ? //使小球按照x軸正方向以每幀*3的單位長度移動

? ? ? ? transform.Translate(Vector3.right * h * moveSpeed * Time.fixedDeltaTime, Space.World);

? ? ? ? if (h < 0)

? ? ? ? {

? ? ? ? ? ? sr.sprite = tankSprite[3];

? ? ? ? ? ? bullectEulerAngles = new Vector3(0, 0, 90);

? ? ? ? }

? ? ? ? else if (h > 0)

? ? ? ? {

? ? ? ? ? ? sr.sprite = tankSprite[1];

? ? ? ? ? ? bullectEulerAngles = new Vector3(0, 0, -90);

? ? ? ? }

? ? ? ? if (Mathf.Abs(h) > 0.05f)

? ? ? ? {

? ? ? ? ? ? moveAudio.clip = tankAudio[1];

? ? ? ? ? ? if (!moveAudio.isPlaying)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? moveAudio.Play();

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? else

? ? ? ? {

? ? ? ? ? ? moveAudio.clip = tankAudio[0];

? ? ? ? ? ? if (!moveAudio.isPlaying)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? moveAudio.Play();

? ? ? ? ? ? }

? ? ? ? }

? ? }

? ? //坦克的死亡方法

? ? private void Die()

? ? {

? ? ? ? //如果無敵

? ? ? ? if (isDefended)

? ? ? ? {

? ? ? ? ? ? return;

? ? ? ? }

? ? ? ? PlayerManager.Instance.isDead = true;

? ? ? ? //產生爆炸特效

? ? ? ? Instantiate(explosionPrefab, transform.position, transform.rotation);

? ? ? ? //死亡

? ? ? ? Destroy(gameObject);

? ? }

}


Bullet.cs

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Bullect : MonoBehaviour {

? ? public float moveSpeed = 10;//子彈速度

? ? public bool isPlayerBullect;//判斷是不是玩家的子彈

// Use this for initialization

void Start () {

}

// Update is called once per frame

void Update () {

? ? ? ? transform.Translate(transform.up * moveSpeed * Time.deltaTime, Space.World);

}

? ? //觸發(fā)器

? ? private void OnTriggerEnter2D(Collider2D collision)

? ? {

? ? ? ? //標簽分類

? ? ? ? switch (collision.tag)

? ? ? ? {

? ? ? ? ? ? case "Tank":

? ? ? ? ? ? ? ? if (!isPlayerBullect)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? collision.SendMessage("Die");

? ? ? ? ? ? ? ? ? ? Destroy(gameObject);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case "Heart":

? ? ? ? ? ? ? ? collision.SendMessage("Die");

? ? ? ? ? ? ? ? Destroy(gameObject);

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case "Enemy":

? ? ? ? ? ? ? ? if (isPlayerBullect)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? collision.SendMessage("Die");

? ? ? ? ? ? ? ? ? ? Destroy(gameObject);

? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case "Wall":

? ? ? ? ? ? ? ? Destroy(collision.gameObject);

? ? ? ? ? ? ? ? Destroy(gameObject);

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case "Barrier":

? ? ? ? ? ? ? ? if (isPlayerBullect)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? collision.SendMessage("PlayAudio");

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? Destroy(gameObject);

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? default:

? ? ? ? ? ? ? ? break;

? ? ? ? }

? ? }

}

Barriar.cs

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Barriar : MonoBehaviour {

? ? //接受聲音

? ? public AudioClip hitAudio;

? ? public void PlayAudio()

? ? {

? ? ? ? //設置子彈射墻的音效

? ? ? ? AudioSource.PlayClipAtPoint(hitAudio, transform.position);

? ? }

}

Heart.cs

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Heart : MonoBehaviour {

? ? //精靈渲染

? ? private SpriteRenderer sr;

? ? //爆炸 游戲對象

? ? public GameObject explosionPrefab;

? ? //死亡 音效

? ? public AudioClip dieAudio;

? ? //精靈對象

? ? public Sprite BrokenSprite;

// Use this for initialization

void Start () {

? ? ? ? sr = GetComponent<SpriteRenderer>();

}

? ? public void Die()

? ? {

? ? ? ? sr.sprite = BrokenSprite;

? ? ? ? //預設實例化

? ? ? ? Instantiate(explosionPrefab, transform.position, transform.rotation);

? ? ? ? //查找isDefeat并賦值

? ? ? ? PlayerManager.Instance.isDefeat = true;

? ? ? ? AudioSource.PlayClipAtPoint(dieAudio, transform.position);

? ? }

}

Born.cs

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Born : MonoBehaviour {

? ? //玩家 游戲對象

? ? public GameObject playerPrefab;

? ? //敵人 游戲對象列表

? ? public GameObject[] enemyPrefabList;

? ? //是否創(chuàng)造玩家

? ? public bool createPlayer;

// Use this for initialization

void Start () {

? ? ? ? //1秒后執(zhí)行某個函數

? ? ? ? Invoke("BornTank", 1f);

? ? ? ? //1秒后再銷毀gameObject

? ? ? ? Destroy(gameObject, 1);

}

// Update is called once per frame

void Update () {

}

? ? private void BornTank()

? ? {

? ? ? ? if (createPlayer)

? ? ? ? {

? ? ? ? ? ? //實例化玩家對象

? ? ? ? ? ? Instantiate(playerPrefab, transform.position, Quaternion.identity);

? ? ? ? }

? ? ? ? else

? ? ? ? {

? ? ? ? ? ? int num = Random.Range(0, 2);

? ? ? ? ? ? //實例化敵人對象

? ? ? ? ? ? Instantiate(enemyPrefabList[num], transform.position, Quaternion.identity);

? ? ? ? }


? ? }

}

Explosion.cs

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Explosion : MonoBehaviour {

// Use this for initialization

void Start () {

? ? ? ? Destroy(gameObject, 0.167f);

}

// Update is called once per frame

void Update () {

}

}


MapCreation.cs

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class MapCreation : MonoBehaviour {

? ? //用來裝飾初始化地圖所需物體的數組。

? ? //0.老家 1.墻 2.障礙 3.出生效果 4.河流 5.草 6.空氣墻

? ? public GameObject[] item;

? ? //已經有東西的位置列表

? ? private List<Vector3> itemPositionList = new List<Vector3>();

? ? private void Awake()

? ? {

? ? ? ? InitMap();

? ? }

? ? private void InitMap()

? ? {

? ? ? ? //實例化老家

? ? ? ? CreateItem(item[0], new Vector3(0, -8, 0), Quaternion.identity);

? ? ? ? //用墻把老家圍起來

? ? ? ? CreateItem(item[1], new Vector3(-1, -8, 0), Quaternion.identity);

? ? ? ? CreateItem(item[1], new Vector3(1, -8, 0), Quaternion.identity);

? ? ? ? for (int i = -1; i < 2; i++)

? ? ? ? {

? ? ? ? ? ? CreateItem(item[1], new Vector3(i, -7, 0), Quaternion.identity);

? ? ? ? }

? ? ? ? //實例化上圍墻

? ? ? ? for (int i = -11; i < 12; i++)

? ? ? ? {

? ? ? ? ? ? CreateItem(item[6], new Vector3(i, 9, 0), Quaternion.identity);

? ? ? ? }

? ? ? ? //實例化下圍墻

? ? ? ? for (int i = -11; i < 12; i++)

? ? ? ? {

? ? ? ? ? ? CreateItem(item[6], new Vector3(i, -9, 0), Quaternion.identity);

? ? ? ? }

? ? ? ? //實例化左圍墻

? ? ? ? for (int i = -8; i < 9; i++)

? ? ? ? {

? ? ? ? ? ? CreateItem(item[6], new Vector3(-11, i, 0), Quaternion.identity);

? ? ? ? }

? ? ? ? //實例化右圍墻

? ? ? ? for (int i = -8; i < 9; i++)

? ? ? ? {

? ? ? ? ? ? CreateItem(item[6], new Vector3(11, i, 0), Quaternion.identity);

? ? ? ? }

? ? ? ? //初始化玩家

? ? ? ? GameObject go = Instantiate(item[3], new Vector3(-2, -8, 0), Quaternion.identity);

? ? ? ? go.GetComponent<Born>().createPlayer = true;

? ? ? ? //產生敵人

? ? ? ? CreateItem(item[3], new Vector3(-10, 8, 0), Quaternion.identity);

? ? ? ? CreateItem(item[3], new Vector3(0, 8, 0), Quaternion.identity);

? ? ? ? CreateItem(item[3], new Vector3(10, 8, 0), Quaternion.identity);

? ? ? ? //4秒后執(zhí)行,5秒CD

? ? ? ? InvokeRepeating("CreateEnemy", 4, 5);

? ? ? ? //實例化地圖

? ? ? ? for (int i = 0; i < 60; i++)

? ? ? ? {

? ? ? ? ? ? CreateItem(item[1], CreateRandomPosition(), Quaternion.identity);

? ? ? ? }

? ? ? ? for (int i = 0; i < 20; i++)

? ? ? ? {

? ? ? ? ? ? CreateItem(item[2], CreateRandomPosition(), Quaternion.identity);

? ? ? ? }

? ? ? ? for (int i = 0; i < 20; i++)

? ? ? ? {

? ? ? ? ? ? CreateItem(item[4], CreateRandomPosition(), Quaternion.identity);

? ? ? ? }

? ? ? ? for (int i = 0; i < 20; i++)

? ? ? ? {

? ? ? ? ? ? CreateItem(item[5], CreateRandomPosition(), Quaternion.identity);

? ? ? ? }

? ? }

? ? private void CreateItem(GameObject createCameObject,Vector3 createPosition,Quaternion createRotation)

? ? {

? ? ? ? GameObject itemGo = Instantiate(createCameObject, createPosition, createRotation);

? ? ? ? itemGo.transform.SetParent(gameObject.transform);

? ? ? ? itemPositionList.Add(createPosition);

? ? }

? ? //產生隨機位置的方法

? ? private Vector3 CreateRandomPosition()

? ? {

? ? ? ? //不生成x=-10,10的兩列,y=-8,8正兩行的位置

? ? ? ? while (true)

? ? ? ? {

? ? ? ? ? ? Vector3 createPosition = new Vector3(Random.Range(-9, 10), Random.Range(-7, 8), 0);

? ? ? ? ? ? if (!HasThePosition(createPosition))

? ? ? ? ? ? {

? ? ? ? ? ? ? ? return createPosition;

? ? ? ? ? ? }


? ? ? ? }

? ? }

? ? //用來判斷位置列表中是否有這個位置

? ? private bool HasThePosition(Vector3 createPos)

? ? {

? ? ? ? for (int i = 0; i < itemPositionList.Count; i++)

? ? ? ? {

? ? ? ? ? ? if (createPos==itemPositionList[i])

? ? ? ? ? ? {

? ? ? ? ? ? ? ? return true;

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return false;

? ? }

? ? //產生敵人的方法

? ? private void CreateEnemy()

? ? {

? ? ? ? int num = Random.Range(0, 3);

? ? ? ? Vector3 EnemyPos = new Vector3();

? ? ? ? if (num==0)

? ? ? ? {

? ? ? ? ? ? EnemyPos = new Vector3(-10, 8, 0);

? ? ? ? }

? ? ? ? else if (num==1)

? ? ? ? {

? ? ? ? ? ? EnemyPos = new Vector3(0, 8, 0);

? ? ? ? }

? ? ? ? else

? ? ? ? {

? ? ? ? ? ? EnemyPos = new Vector3(10, 8, 0);

? ? ? ? }

? ? ? ? CreateItem(item[3], EnemyPos, Quaternion.identity);

? ? }

}


Option.cs

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.SceneManagement;

public class Option : MonoBehaviour {

? ? private int choice = 1;

? ? public Transform posOne;

? ? public Transform posTwo;

// Use this for initialization

void Start () {

}

// Update is called once per frame

void Update () {

? ? ? ? if (Input.GetKeyDown(KeyCode.W))

? ? ? ? {

? ? ? ? ? ? choice = 1;

? ? ? ? ? ? transform.position = posOne.position;

? ? ? ? }

? ? ? ? else if (Input.GetKeyDown(KeyCode.S))

? ? ? ? {

? ? ? ? ? ? choice = 2;

? ? ? ? ? ? transform.position = posTwo.position;

? ? ? ? }

? ? ? ? if (choice==1&&Input.GetKeyDown(KeyCode.Space))

? ? ? ? {

? ? ? ? ? ? SceneManager.LoadScene(1);

? ? ? ? }

}

}


PlayManager.cs

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;

using UnityEngine.SceneManagement;

public class PlayerManager : MonoBehaviour {

? ? //生命值

? ? public int lifeValue = 3;

? ? //得分

? ? public int playerScore = 0;

? ? public bool isDead;

? ? public bool isDefeat;

? ? //引用

? ? public GameObject born;

? ? public Text playerScoreText;

? ? public Text PlayerLifeValueText;

? ? public GameObject isDefeatUI;

? ? //單例

? ? private static PlayerManager instance;

? ? public static PlayerManager Instance

? ? {

? ? ? ? get

? ? ? ? {

? ? ? ? ? ? return instance;

? ? ? ? }

? ? ? ? set

? ? ? ? {

? ? ? ? ? ? instance = value;

? ? ? ? }

? ? }

? ? private void Awake()

? ? {

? ? ? ? Instance = this;

? ? }

? ? // Use this for initialization

? ? void Start () {

}

// Update is called once per frame

void Update () {

? ? ? ? if (isDefeat)

? ? ? ? {

? ? ? ? ? ? isDefeatUI.SetActive(true);

? ? ? ? ? ? Invoke("ReturnToTheMainMenu", 3);

? ? ? ? ? ? return;

? ? ? ? }

? ? ? ? if (isDead)

? ? ? ? {

? ? ? ? ? ? Recover();

? ? ? ? }

? ? ? ? playerScoreText.text = playerScore.ToString();

? ? ? ? PlayerLifeValueText.text = lifeValue.ToString();

}

? ? private void Recover()

? ? {

? ? ? ? if (lifeValue<=0)

? ? ? ? {

? ? ? ? ? ? //游戲失敗,返回主界面

? ? ? ? ? ? isDefeat = true;

? ? ? ? ? ? Invoke("ReturnToTheMainMenu", 3);

? ? ? ? }

? ? ? ? else

? ? ? ? {

? ? ? ? ? ? lifeValue--;

? ? ? ? ? ? GameObject go = Instantiate(born, new Vector3(-2, -8, 0), Quaternion.identity);

? ? ? ? ? ? go.GetComponent<Born>().createPlayer = true;

? ? ? ? ? ? isDead = false;

? ? ? ? }

? ? }

? ? private void ReturnToTheMainMenu()

? ? {

? ? ? ? SceneManager.LoadScene(0);

? ? }

}

?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容