BotにHPを設定する

BotHP
using UnityEngine;
public class BotHP : MonoBehaviour
{
public int HP { get; set; }
public GameObject effectPrefab;
public AudioClip sound;
void OnTriggerEnter(Collider other)
{
if (this.gameObject.tag == "RedBot")
{
if (other.gameObject.tag == "BlueBot")
{
Damage();
}
}
else
{
if (other.gameObject.tag == "RedBot")
{
Damage();
}
}
}
void Damage()
{
HP -= 1;
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
if (HP < 1)
{
Destroy(this.gameObject);
}
}
}
DBにHPの項目を追加する
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;
}









BotにデータベースのHPを設定する
using System.Collections;
using UnityEngine;
public class BotSpawn : MonoBehaviour
{
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;
GameObject bot = Instantiate(botPrefab, spawnPoint.transform.position, transform.rotation);
bot.GetComponent<BotMove>().moveSpeed = speed;
// ★追加
bot.GetComponent<BotHP>().HP = HP;
yield return new WaitForSeconds(1f);
}
yield return new WaitForSeconds(2f);
}
}
}
【Unity6版】TowerDX(全 回)
他のコースを見る
BotHP
using UnityEngine;
public class BotHP : MonoBehaviour
{
public int HP { get; set; }
public GameObject effectPrefab;
public AudioClip sound;
void OnTriggerEnter(Collider other)
{
if (this.gameObject.tag == "RedBot")
{
if (other.gameObject.tag == "BlueBot")
{
Damage();
}
}
else
{
if (other.gameObject.tag == "RedBot")
{
Damage();
}
}
}
void Damage()
{
HP -= 1;
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
if (HP < 1)
{
Destroy(this.gameObject);
}
}
}
DBにHPの項目を追加する
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;
}









BotにデータベースのHPを設定する
using System.Collections;
using UnityEngine;
public class BotSpawn : MonoBehaviour
{
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;
GameObject bot = Instantiate(botPrefab, spawnPoint.transform.position, transform.rotation);
bot.GetComponent<BotMove>().moveSpeed = speed;
// ★追加
bot.GetComponent<BotHP>().HP = HP;
yield return new WaitForSeconds(1f);
}
yield return new WaitForSeconds(2f);
}
}
}
BotにHPを設定する