欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > C# >内容正文

C#

【Unity2D入门教程】简单制作战机弹幕射击游戏④C#编写 敌人按指定路径以及敌人生成点脚本

发布时间:2024/3/26 C# 85 豆豆
生活随笔 收集整理的这篇文章主要介绍了 【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#编写 敌人按指定路径以及敌人生成点脚本的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。