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

タイマーを使って発射間隔を調整する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShotShell : MonoBehaviour
{
public GameObject shellPrefab;
public float shotSpeed;
public AudioClip shotSound;
// ★★(追加)
private float timeBetweenShot = 0.35f;
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);
}
}
}

BattleTank(基礎/全31回)
他のコースを見る
タイマーを使って発射間隔を調整する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShotShell : MonoBehaviour
{
public GameObject shellPrefab;
public float shotSpeed;
public AudioClip shotSound;
// ★★(追加)
private float timeBetweenShot = 0.35f;
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);
}
}
}

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