【Unity2D入门教程】简单制作战机弹幕射击游戏④C#编写 敌人按指定路径以及敌人生成点脚本
生活随笔
收集整理的這篇文章主要介紹了
【Unity2D入门教程】简单制作战机弹幕射击游戏④C#编写 敌人按指定路径以及敌人生成点脚本
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言:
我們前面忘記設置的當敵機和子彈碰到特定的位置(指屏幕外的)就會自動銷毀
掛載的腳本Sherred如下
using System.Collections; using System.Collections.Generic; using UnityEngine;public class Shredder : MonoBehaviour {private void OnTriggerEnter2D(Collider2D collision){Destroy(collision.gameObject);} }正題:
ok我們現在開始制作敵人按指定路徑以及敵人生成點腳本
首先你先創建一個空對象Path再給它幾個子空對象,這是敵機需要移動的方向,然后用空物體的不同位置比如斜角的移動,只需要設置幾個點就行,然后再把Path拖到我們創建的Paths的文上????????
然后我們需要創建數據文件來保存有關敵機的各種數據
using System.Collections; using System.Collections.Generic; using UnityEngine;[CreateAssetMenu(menuName ="Enemy Wave Config")] public class Wayconfig : ScriptableObject {[SerializeField] GameObject enemyPrefab; //敵機預設體[SerializeField] GameObject pathPrefab; //路徑Path[SerializeField] float timeBetweenSpawns = 0.5f; //敵機生成的間隔時間[SerializeField] float spawnRandomFactor = 0.3f; //[SerializeField] int numberOfEnemies = 5; //生成多少個敵機[SerializeField] float moveSpeed = 2f; //敵機的移動速度public GameObject GetEnemyPrefab(){return enemyPrefab;}public List<Transform> GetWayPoints() //返回一個為Transform類型的鏈表即路徑Path的子對象我們設置的那些路徑點{var waveWayPoints = new List<Transform>();foreach (Transform child in pathPrefab.transform){waveWayPoints.Add(child);}return waveWayPoints;}public float GetTimeBetweenSpawns(){return timeBetweenSpawns;}public float GetSpawnRandomFactor(){return spawnRandomFactor;}public int GetNumberOfEnemies(){return numberOfEnemies;}public float GetMoveSpeed(){return moveSpeed;} }當我們點擊Create的時候就可以看到最頂端我們可以創建記錄敵機數據的表,我們創建多幾個并統一叫Wave統一放在名字叫Waves的文件夾
?
?我們把我們創建的Enemy和Path的Prefab都設置后下面那四個數字也都可以動
保存好數據后我們還需要一個敵人生成器EnemySpawner,先創建一個同名的空對象
代碼如下
using System.Collections; using System.Collections.Generic; using UnityEngine;public class EnemySpawner : MonoBehaviour {[SerializeField] bool looping = false; //控制是否循環執行生成wavepublic List<Wayconfig> waveConfigs; //創建waveconfig類型的鏈表int startingWave = 0;IEnumerator Start(){do{yield return StartCoroutine(SpawnAllWaves());} while (looping);}IEnumerator SpawnAllWaves(){for (int waveIndex = 0; waveIndex < waveConfigs.Count; waveIndex++){var currentWave = waveConfigs[waveIndex]; //決定生成的是第index波yield return StartCoroutine(SpawnAllEnemiesInWave(currentWave));}}IEnumerator SpawnAllEnemiesInWave(Wayconfig waveConfig){for (int enemyCounts = 0; enemyCounts < waveConfig.GetNumberOfEnemies(); enemyCounts++) //當小于最大敵人生成數量的時候就循環生成{var newEnemy = Instantiate(waveConfig.GetEnemyPrefab(), waveConfig.GetWayPoints()[0].transform.position, Quaternion.identity); //GetWayPoints()[0]即第一個標記點newEnemy.GetComponent<EnemyPathing>().SetWaveConfig(waveConfig);yield return new WaitForSeconds(waveConfig.GetTimeBetweenSpawns());}} }?寫完后我們發現怎么沒有給敵機移動的腳本呢,于是我們要創建一個EnemyPathing供Enemy移動
using System.Collections; using System.Collections.Generic; using UnityEngine;public class EnemyPathing : MonoBehaviour {Wayconfig waveConfig;List<Transform> waypoints;int wayPointIndex = 0;void Start(){waypoints = waveConfig.GetWayPoints();transform.position = waypoints[wayPointIndex].transform.position; }// Update is called once per framevoid Update(){Move();}public void SetWaveConfig(Wayconfig waveConfig){this.waveConfig = waveConfig;}private void Move() //控制敵機的移動{if (wayPointIndex <= waypoints.Count - 1){var targetPosition = waypoints[wayPointIndex].transform.position;var movementThisFrame = waveConfig.GetMoveSpeed() * Time.deltaTime;transform.position = Vector2.MoveTowards(transform.position, targetPosition, movementThisFrame);if (transform.position == targetPosition){wayPointIndex++;}}else{Destroy(gameObject);}} }然后綁到Enemy的Prefab上?
學習產出:
可見敵人已經按照我們制定的順序出場了
總結
以上是生活随笔為你收集整理的【Unity2D入门教程】简单制作战机弹幕射击游戏④C#编写 敌人按指定路径以及敌人生成点脚本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LaTeX教程(三)——文档格式排版
- 下一篇: 单片机中灯泡显示miss_基于单片机的交