敵の攻撃を作る①(一定時間ごとに砲弾を発射する)














一定時間ごとに砲弾を発射する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyShotShell : MonoBehaviour
{
public float shotSpeed;
public GameObject enemyShellPrefab;
public AudioClip shotSound;
private int count;
private void Update()
{
count += 1;
// 「%」と「==」の意味を考えよう(ポイント)
if(count % 100 == 0)
{
GameObject enemyShell = Instantiate(enemyShellPrefab, transform.position, Quaternion.identity);
Rigidbody enemyShellRb = enemyShell.GetComponent<Rigidbody>();
// forwardはZ軸方向(青軸方向)・・・>この方向に力を加える。
enemyShellRb.AddForce(transform.forward * shotSpeed);
AudioSource.PlayClipAtPoint(shotSound, transform.position);
Destroy(enemyShell, 3.0f);
}
}
}







【2021版】BattleTank(基礎/全33回)
他のコースを見る













一定時間ごとに砲弾を発射する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyShotShell : MonoBehaviour
{
public float shotSpeed;
public GameObject enemyShellPrefab;
public AudioClip shotSound;
private int count;
private void Update()
{
count += 1;
// 「%」と「==」の意味を考えよう(ポイント)
if(count % 100 == 0)
{
GameObject enemyShell = Instantiate(enemyShellPrefab, transform.position, Quaternion.identity);
Rigidbody enemyShellRb = enemyShell.GetComponent<Rigidbody>();
// forwardはZ軸方向(青軸方向)・・・>この方向に力を加える。
enemyShellRb.AddForce(transform.forward * shotSpeed);
AudioSource.PlayClipAtPoint(shotSound, transform.position);
Destroy(enemyShell, 3.0f);
}
}
}







敵の攻撃を作る①(一定時間ごとに砲弾を発射する)