連射を不可にする(タイマーで発射間隔を調整する)
![6c8cbae2 bfe2 4a74 a9c6 bd33cc3bd266](https://codegenius.org/uploads/slide/image/2107/6c8cbae2-bfe2-4a74-a9c6-bd33cc3bd266.jpeg)
タイマーを使って発射間隔を調整する
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);
}
}
}
![E3feebfd 8a68 49b0 987f 7046a278d8b5](https://codegenius.org/uploads/slide/image/2108/e3feebfd-8a68-49b0-987f-7046a278d8b5.jpeg)
BattleTank(基礎/全31回)
他のコースを見る![6c8cbae2 bfe2 4a74 a9c6 bd33cc3bd266](https://codegenius.org/uploads/slide/image/2107/6c8cbae2-bfe2-4a74-a9c6-bd33cc3bd266.jpeg)
タイマーを使って発射間隔を調整する
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);
}
}
}
![E3feebfd 8a68 49b0 987f 7046a278d8b5](https://codegenius.org/uploads/slide/image/2108/e3feebfd-8a68-49b0-987f-7046a278d8b5.jpeg)
連射を不可にする(タイマーで発射間隔を調整する)