弾切れの発生①(一定時間連射で弾切れ)


弾切れの発生
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FireMissile : MonoBehaviour
{
public GameObject missilePrefab;
public float missileSpeed;
public AudioClip fireSound;
private int timeCount;
// ★追加(弾切れ発生)
private int maxPower = 100;
private int shotPower;
// ★追加(弾切れ発生)
private void Start()
{
shotPower = maxPower;
}
void Update()
{
timeCount += 1;
if (Input.GetButton("Jump"))
{
// ★追加(弾切れ発生)
// ここのロジックをよく復習すること(重要ポイント)
if(shotPower <= 0)
{
return;
}
// ★追加(弾切れ発生)
shotPower -= 1;
if (timeCount % 5 == 0)
{
GameObject missile = Instantiate(missilePrefab, transform.position, Quaternion.identity);
Rigidbody missileRb = missile.GetComponent<Rigidbody>();
missileRb.AddForce(transform.forward * missileSpeed);
AudioSource.PlayClipAtPoint(fireSound, transform.position);
Destroy(missile, 2.0f);
}
}
}
}

【2019版】Danmaku Ⅱ(基礎2/全38回)
他のコースを見る

弾切れの発生
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FireMissile : MonoBehaviour
{
public GameObject missilePrefab;
public float missileSpeed;
public AudioClip fireSound;
private int timeCount;
// ★追加(弾切れ発生)
private int maxPower = 100;
private int shotPower;
// ★追加(弾切れ発生)
private void Start()
{
shotPower = maxPower;
}
void Update()
{
timeCount += 1;
if (Input.GetButton("Jump"))
{
// ★追加(弾切れ発生)
// ここのロジックをよく復習すること(重要ポイント)
if(shotPower <= 0)
{
return;
}
// ★追加(弾切れ発生)
shotPower -= 1;
if (timeCount % 5 == 0)
{
GameObject missile = Instantiate(missilePrefab, transform.position, Quaternion.identity);
Rigidbody missileRb = missile.GetComponent<Rigidbody>();
missileRb.AddForce(transform.forward * missileSpeed);
AudioSource.PlayClipAtPoint(fireSound, transform.position);
Destroy(missile, 2.0f);
}
}
}
}

弾切れの発生①(一定時間連射で弾切れ)