タワーディフェンス講座_敵のスポーン
- 記述したコード
- This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
using System.Collections; using System.Collections.Generic; using UnityEngine; using System; public class Enemy : MonoBehaviour { [NonSerialized] public EnemyHP enemyHP; //移動速度設定用 [SerializeField] private float setMoveSpeed = 3f; //移動速度 private float moveSpeed; //MovePoint格納変数 [NonSerialized] public MovePoint movePoint; //向かうべきポイントの番号 private int currentMovePointIndex; //CurrentPointPositionが呼ばれるたびに後半の処理がおこなわれる public Vector3 CurrentPointPosition => movePoint.GetMovePointPosition(currentMovePointIndex); void Start() { //格納 enemyHP = GetComponent<EnemyHP>(); //向かうべきポイントを設定 currentMovePointIndex = 0; //移動スピードの設定 SetMoveSpeed(); //指定のコンポーネントが //ついているオブジェクトを探して格納する(一時的に使用) //movePoint = FindObjectOfType<MovePoint>().GetComponent<MovePoint>(); } public void SetMoveSpeed() { moveSpeed = setMoveSpeed; } /// <summary> /// 移動スピードを0にする /// </summary> public void StopMovement() { moveSpeed = 0f; } void Update() { Move(); //現在の目的ポイントに到着しているか if(NextPointReached()) { //次のポイントに更新 UpdatePointIndex(); } } /// <summary> /// 目的地を設定する数値を更新する /// </summary> private void UpdatePointIndex() { //最後でないならポイントを更新する if (currentMovePointIndex < movePoint.points.Length - 1) { currentMovePointIndex++; } } /// <summary> /// 次のポイントに到着しているか確認する /// </summary> /// <returns></returns> private bool NextPointReached() { //次のポイントまでの残りの距離を変数に格納 float distance = (transform.position - CurrentPointPosition).magnitude; //判定 if (distance < 0.1) { //到着判定 return true; } //到着していない判定 return false; } private void Move() { //ポジションを移動 transform.position = Vector3.MoveTowards( transform.position, CurrentPointPosition, moveSpeed * Time.deltaTime); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersusing System; using System.Collections; using System.Collections.Generic; using UnityEngine; //スポーンモード public enum SpawnModes { constant,//一定 Random//ランダム } public class Spawner : MonoBehaviour { //スポーンモード選択 [SerializeField] private SpawnModes spawnMode = SpawnModes.constant; //最短のスポーン間隔 [SerializeField] private float minRandomDelay; //最長のスポーン間隔 [SerializeField] private float maxRandomDelay; //一定モードのスポーン時間 [SerializeField] private float constantSpawnTime; //スポーンさせる数を設定する数 [SerializeField] private int enemyCount = 10; //タイマー変数 private float spawnTimer; //スポーンさせた数(数を追加していく) private float spawned; //enemyのオブジェクトプール用 private ObjectPooler pooler; //MovePoint格納用 enemyに渡すためにここで格納する private MovePoint movePoint; private void Start() { //変数にコンポーネント格納 pooler = GetComponent<ObjectPooler>(); movePoint = GetComponent<MovePoint>(); } void Update() { //spawnTimerの時間を減らす spawnTimer -= Time.deltaTime; //確認 if (spawnTimer < 0) { //次のスポーンまでの時間を設定 spawnTimer = GetSpawnDelay(); //スポーン上限の確認 if (spawned < enemyCount) { //生成済みの数を追加 spawned++; //敵を生成 SpawnEnemy(); } } } /// <summary> /// 敵を生成する /// </summary> private void SpawnEnemy() { //プールから取得して変数に格納 GameObject newInstance = pooler.GetObjectFromPool(); //エネミーの初期設定 SetEnemy(newInstance); //表示する newInstance.SetActive(true); } private void SetEnemy(GameObject newInstance) { //enemyでmovePoint使えるように格納している Enemy enemy = newInstance.GetComponent<Enemy>(); enemy.movePoint = movePoint; //生成位置をこのオブジェクトの位置に設定 enemy.transform.position = transform.position; //スピードの設定 enemy.SetMoveSpeed(); } /// <summary> /// 一定orランダムの数値を返す /// </summary> /// <returns></returns> private float GetSpawnDelay() { if (spawnMode == SpawnModes.constant) { return constantSpawnTime; } else//ランダム { //引数の間からランダムな数値を選んで返す return UnityEngine.Random.Range(minRandomDelay, maxRandomDelay); } } }
YouTubeでゲーム開発系の動画上げてます