連射を不可にする(タイマーで発射間隔を調整する)

タイマーで発射間隔を調整する
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;
void Update()
{
// ★★(追加)
// タイマーの時間を動かす
timer += Time.deltaTime;
// ★★(追加)
// 条件を追加する。
if (Input.GetKeyDown(KeyCode.Space) && timer > timeBetweenShot)
{
// ★★(追加)
// タイマーの時間を0に戻す。
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;
void Update()
{
// ★★(追加)
// タイマーの時間を動かす
timer += Time.deltaTime;
// ★★(追加)
// 条件を追加する。
if (Input.GetKeyDown(KeyCode.Space) && timer > timeBetweenShot)
{
// ★★(追加)
// タイマーの時間を0に戻す。
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);
}
}
}

連射を不可にする(タイマーで発射間隔を調整する)