砲弾の発射回数に制限を加える(弾切れを発生させる)
弾切れの発生
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;
// ★追加
public int shotCount;
void Update()
{
timer += Time.deltaTime;
// ★条件(&& shotCount > 0)の追加
if (Input.GetKeyDown(KeyCode.Space) && timer > interval && 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);
}
}
}
【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;
// ★追加
public int shotCount;
void Update()
{
timer += Time.deltaTime;
// ★条件(&& shotCount > 0)の追加
if (Input.GetKeyDown(KeyCode.Space) && timer > interval && 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);
}
}
}
砲弾の発射回数に制限を加える(弾切れを発生させる)