特殊能力の追加その1(敵の速度を低下させる。データベースにTagのパラメータを追加)

Tag項目の追加
using System;
using UnityEngine;
[Serializable]
public class SpawnStatus
{
[Range(1, 8)]
public int repeatCount;
[Range(1, 10)]
public int moveSpeed;
[Range(1, 5)]
public int HP;
public int cost;
// ★追加
public string tag;
public void CalculateCost()
{
cost = repeatCount * moveSpeed * HP * 10;
}
}

データベースで設定したタグに変更する
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public class BotSpawn : MonoBehaviour
{
public enum TeamType { Blue, Red };
public TeamType team;
public PointManager pm;
public SpawnDatabase database;
public GameObject botPrefab;
public GameObject spawnPoint;
void Start()
{
StartCoroutine(nameof(SpawnRoutine));
}
private IEnumerator SpawnRoutine()
{
yield return new WaitForSeconds(2f);
for (int i = 0; i < database.spawnList.Count; i++)
{
for (int j = 0; j < database.spawnList[i].repeatCount; j++)
{
int speed = database.spawnList[i].moveSpeed;
int HP = database.spawnList[i].HP;
int botPrice = speed * HP * 10;
if (team == TeamType.Blue)
{
if (pm.bluePoint < botPrice)
{
SceneManager.LoadScene("BlueLose");
yield break;
}
pm.ReduceBluePoint(botPrice);
}
else
{
if (pm.redPoint < botPrice)
{
SceneManager.LoadScene("RedLose");
yield break;
}
pm.ReduceRedPoint(botPrice);
}
GameObject bot = Instantiate(botPrefab, spawnPoint.transform.position, transform.rotation);
bot.GetComponent<BotMove>().moveSpeed = speed;
bot.GetComponent<BotHP>().HP = HP;
// ★追加
// データベースで設定したタグに変更する
// null及び空文字("")の両方チェック(テクニック)
if(!string.IsNullOrEmpty(database.spawnList[i].tag))
{
bot.tag = database.spawnList[i].tag;
}
yield return new WaitForSeconds(1f);
}
yield return new WaitForSeconds(2f);
}
}
}

Botの移動速度を低下させる
using UnityEngine;
public class BotMove : MonoBehaviour
{
public float moveSpeed;
private float maxDis = 0.75f;
private string tagName;
void Update()
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
RaycastHit hit;
Ray ray = new Ray(transform.position, transform.forward);
Debug.DrawRay(transform.position, transform.forward * maxDis, Color.red);
if (Physics.Raycast(ray, out hit, maxDis))
{
GameObject target = hit.collider.gameObject;
tagName = target.tag;
if (tagName == "Right")
{
transform.localRotation = Quaternion.Euler(0, transform.eulerAngles.y + 90, 0);
}
else if (tagName == "Left")
{
transform.localRotation = Quaternion.Euler(0, transform.eulerAngles.y + 270, 0);
}
// ★追加
else if (tagName == "Slow")
{
moveSpeed = 1;
}
}
}
}



【Unity6版】TowerDX(全 回)
他のコースを見る
Tag項目の追加
using System;
using UnityEngine;
[Serializable]
public class SpawnStatus
{
[Range(1, 8)]
public int repeatCount;
[Range(1, 10)]
public int moveSpeed;
[Range(1, 5)]
public int HP;
public int cost;
// ★追加
public string tag;
public void CalculateCost()
{
cost = repeatCount * moveSpeed * HP * 10;
}
}

データベースで設定したタグに変更する
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public class BotSpawn : MonoBehaviour
{
public enum TeamType { Blue, Red };
public TeamType team;
public PointManager pm;
public SpawnDatabase database;
public GameObject botPrefab;
public GameObject spawnPoint;
void Start()
{
StartCoroutine(nameof(SpawnRoutine));
}
private IEnumerator SpawnRoutine()
{
yield return new WaitForSeconds(2f);
for (int i = 0; i < database.spawnList.Count; i++)
{
for (int j = 0; j < database.spawnList[i].repeatCount; j++)
{
int speed = database.spawnList[i].moveSpeed;
int HP = database.spawnList[i].HP;
int botPrice = speed * HP * 10;
if (team == TeamType.Blue)
{
if (pm.bluePoint < botPrice)
{
SceneManager.LoadScene("BlueLose");
yield break;
}
pm.ReduceBluePoint(botPrice);
}
else
{
if (pm.redPoint < botPrice)
{
SceneManager.LoadScene("RedLose");
yield break;
}
pm.ReduceRedPoint(botPrice);
}
GameObject bot = Instantiate(botPrefab, spawnPoint.transform.position, transform.rotation);
bot.GetComponent<BotMove>().moveSpeed = speed;
bot.GetComponent<BotHP>().HP = HP;
// ★追加
// データベースで設定したタグに変更する
// null及び空文字("")の両方チェック(テクニック)
if(!string.IsNullOrEmpty(database.spawnList[i].tag))
{
bot.tag = database.spawnList[i].tag;
}
yield return new WaitForSeconds(1f);
}
yield return new WaitForSeconds(2f);
}
}
}

Botの移動速度を低下させる
using UnityEngine;
public class BotMove : MonoBehaviour
{
public float moveSpeed;
private float maxDis = 0.75f;
private string tagName;
void Update()
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
RaycastHit hit;
Ray ray = new Ray(transform.position, transform.forward);
Debug.DrawRay(transform.position, transform.forward * maxDis, Color.red);
if (Physics.Raycast(ray, out hit, maxDis))
{
GameObject target = hit.collider.gameObject;
tagName = target.tag;
if (tagName == "Right")
{
transform.localRotation = Quaternion.Euler(0, transform.eulerAngles.y + 90, 0);
}
else if (tagName == "Left")
{
transform.localRotation = Quaternion.Euler(0, transform.eulerAngles.y + 270, 0);
}
// ★追加
else if (tagName == "Slow")
{
moveSpeed = 1;
}
}
}
}



特殊能力の追加その1(敵の速度を低下させる。データベースにTagのパラメータを追加)