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

タイマーで発射間隔を調整する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShotShell : MonoBehaviour
{
public float shotSpeed;
public GameObject shellPrefab;
public AudioClip shotSound;
// ★★追加
private float interval = 0.75f;
private float timer = 0;
void Update()
{
// ★★追加
// タイマーの時間を動かす。
timer += Time.deltaTime;
// ★★条件の追加
// 「&&」の意味を復習しよう。
if (Input.GetKeyDown(KeyCode.Space) && timer > interval)
{
// ★★追加
// タイマーの時間を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);
}
}
}

【2021版】BattleTank(基礎/全33回)
他のコースを見る
タイマーで発射間隔を調整する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShotShell : MonoBehaviour
{
public float shotSpeed;
public GameObject shellPrefab;
public AudioClip shotSound;
// ★★追加
private float interval = 0.75f;
private float timer = 0;
void Update()
{
// ★★追加
// タイマーの時間を動かす。
timer += Time.deltaTime;
// ★★条件の追加
// 「&&」の意味を復習しよう。
if (Input.GetKeyDown(KeyCode.Space) && timer > interval)
{
// ★★追加
// タイマーの時間を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);
}
}
}

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