砲弾の発射回数に制限を加える(弾切れを発生させる)

弾切れの発生
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShotShell : MonoBehaviour
{
public float shotSpeed;
[SerializeField]
private GameObject shellPrefab = null;
[SerializeField]
private AudioClip shotSound = null;
private float timeBetweenShot = 0.75f;
private float timer;
// ★追加
public int shotCount;
void Update()
{
timer += Time.deltaTime;
// ★条件に&& shotCount > 0を追加
if (Input.GetKeyDown(KeyCode.Space) && timer > timeBetweenShot && shotCount > 0)
{
// ★追加
shotCount -= 1;
timer = 0.0f;
GameObject shell = Instantiate(shellPrefab, transform.position, Quaternion.identity);
Rigidbody shellRb = shell.GetComponent<Rigidbody>();
shellRb.AddForce(transform.forward * shotSpeed);
Destroy(shell, 3.0f);
AudioSource.PlayClipAtPoint(shotSound, transform.position);
}
}
}


【2019版】BattleTank(基礎/全38回)
他のコースを見る
弾切れの発生
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShotShell : MonoBehaviour
{
public float shotSpeed;
[SerializeField]
private GameObject shellPrefab = null;
[SerializeField]
private AudioClip shotSound = null;
private float timeBetweenShot = 0.75f;
private float timer;
// ★追加
public int shotCount;
void Update()
{
timer += Time.deltaTime;
// ★条件に&& shotCount > 0を追加
if (Input.GetKeyDown(KeyCode.Space) && timer > timeBetweenShot && shotCount > 0)
{
// ★追加
shotCount -= 1;
timer = 0.0f;
GameObject shell = Instantiate(shellPrefab, transform.position, Quaternion.identity);
Rigidbody shellRb = shell.GetComponent<Rigidbody>();
shellRb.AddForce(transform.forward * shotSpeed);
Destroy(shell, 3.0f);
AudioSource.PlayClipAtPoint(shotSound, transform.position);
}
}
}


砲弾の発射回数に制限を加える(弾切れを発生させる)