敵の攻撃を作るその1







敵の攻撃の作成
using UnityEngine;
// ★追加
using System.Collections;
public class EnemyAttack_1 : MonoBehaviour
{
public GameObject enemyShellPrefab;
public AudioClip shotSound;
private SphereCollider sCollider;
// 敵の攻撃力(インスペクターで調整できるようにする)
// (1)砲弾の速度
public float shotSpeed;
// (2)1ターンの攻撃回数
public int attackNum;
// (3)1ターンの攻撃間隔
public float attackInterval;
// (4)探知能力(Colliderの半径)
public float serachArea;
private void Start()
{
sCollider = GetComponent<SphereCollider>();
// Collideの半径の設定
sCollider.radius = serachArea;
// Is Triggerをオンにする
sCollider.isTrigger = true;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "Tank")
{
// 親オブジェクトがターゲットの方に向く。
transform.root.LookAt(other.transform);
StartCoroutine(Attack());
}
}
// コルーチン
private IEnumerator Attack()
{
for (int i = 0; i < attackNum; i++)
{
GameObject enemyShell = Instantiate(enemyShellPrefab, transform.position, Quaternion.identity);
Rigidbody enemyShellRb = enemyShell.GetComponent<Rigidbody>();
enemyShellRb.useGravity = false;
enemyShellRb.AddForce(transform.forward * shotSpeed);
AudioSource.PlayClipAtPoint(shotSound, transform.position);
Destroy(enemyShell, 5.0f);
yield return new WaitForSeconds(attackInterval);
}
}
}





【Unity6版】BattleTank(全31回)
他のコースを見る






敵の攻撃の作成
using UnityEngine;
// ★追加
using System.Collections;
public class EnemyAttack_1 : MonoBehaviour
{
public GameObject enemyShellPrefab;
public AudioClip shotSound;
private SphereCollider sCollider;
// 敵の攻撃力(インスペクターで調整できるようにする)
// (1)砲弾の速度
public float shotSpeed;
// (2)1ターンの攻撃回数
public int attackNum;
// (3)1ターンの攻撃間隔
public float attackInterval;
// (4)探知能力(Colliderの半径)
public float serachArea;
private void Start()
{
sCollider = GetComponent<SphereCollider>();
// Collideの半径の設定
sCollider.radius = serachArea;
// Is Triggerをオンにする
sCollider.isTrigger = true;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "Tank")
{
// 親オブジェクトがターゲットの方に向く。
transform.root.LookAt(other.transform);
StartCoroutine(Attack());
}
}
// コルーチン
private IEnumerator Attack()
{
for (int i = 0; i < attackNum; i++)
{
GameObject enemyShell = Instantiate(enemyShellPrefab, transform.position, Quaternion.identity);
Rigidbody enemyShellRb = enemyShell.GetComponent<Rigidbody>();
enemyShellRb.useGravity = false;
enemyShellRb.AddForce(transform.forward * shotSpeed);
AudioSource.PlayClipAtPoint(shotSound, transform.position);
Destroy(enemyShell, 5.0f);
yield return new WaitForSeconds(attackInterval);
}
}
}





敵の攻撃を作るその1