ステージ3の作成/味方車両の護衛








味方車両をロックオンする
using UnityEngine;
using System.Collections;
public class EnemyAttack_1 : MonoBehaviour
{
public GameObject enemyShellPrefab;
public AudioClip shotSound;
private SphereCollider sCollider;
public float shotSpeed;
public int attackNum;
public float attackInterval;
public float serachArea;
private void Start()
{
sCollider = GetComponent<SphereCollider>();
sCollider.radius = serachArea;
sCollider.isTrigger = true;
}
private void OnTriggerEnter(Collider other)
{
// ★改良
// 「|| other.gameObject.name == "SupplyCar"」のコードを追加
if (other.gameObject.name == "Tank" || other.gameObject.name == "SupplyCar")
{
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);
}
}
}

味方車両がゴール地点到達でクリアー
using UnityEngine;
public class GameManager_3 : MonoBehaviour
{
public GameObject clearCanvas;
public AudioClip clearSound;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "SupplyCar")
{
clearCanvas.SetActive(true);
AudioSource.PlayClipAtPoint(clearSound, Camera.main.transform.position);
}
}
}






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







味方車両をロックオンする
using UnityEngine;
using System.Collections;
public class EnemyAttack_1 : MonoBehaviour
{
public GameObject enemyShellPrefab;
public AudioClip shotSound;
private SphereCollider sCollider;
public float shotSpeed;
public int attackNum;
public float attackInterval;
public float serachArea;
private void Start()
{
sCollider = GetComponent<SphereCollider>();
sCollider.radius = serachArea;
sCollider.isTrigger = true;
}
private void OnTriggerEnter(Collider other)
{
// ★改良
// 「|| other.gameObject.name == "SupplyCar"」のコードを追加
if (other.gameObject.name == "Tank" || other.gameObject.name == "SupplyCar")
{
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);
}
}
}

味方車両がゴール地点到達でクリアー
using UnityEngine;
public class GameManager_3 : MonoBehaviour
{
public GameObject clearCanvas;
public AudioClip clearSound;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "SupplyCar")
{
clearCanvas.SetActive(true);
AudioSource.PlayClipAtPoint(clearSound, Camera.main.transform.position);
}
}
}






ステージ3の作成/味方車両の護衛