弾切れの発生①(一定時間連射で弾切れ)
弾切れを発生させる
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;
// ★追加(弾切れ発生)
void Start()
{
shotPower = maxPower;
}
void Update()
{
timeCount += 1;
if (Input.GetButton("Fire1"))
{
// ★追加(弾切れ発生)
// ここのロジックをよく復習すること(重要ポイント)
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);
}
}
}
}
弾切れを発生させる
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;
// ★追加(弾切れ発生)
void Start()
{
shotPower = maxPower;
}
void Update()
{
timeCount += 1;
if (Input.GetButton("Fire1"))
{
// ★追加(弾切れ発生)
// ここのロジックをよく復習すること(重要ポイント)
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);
}
}
}
}
弾切れの発生①(一定時間連射で弾切れ)