弾切れの発生①(一定時間連射で弾切れ)
![761317e7 c4e2 4695 80d7 042b5293878e](https://codegenius.org/uploads/slide/image/1710/761317e7-c4e2-4695-80d7-042b5293878e.jpeg)
![Ab8a62af e233 4381 b482 76684ae38e6b](https://codegenius.org/uploads/slide/image/1711/ab8a62af-e233-4381-b482-76684ae38e6b.jpeg)
弾切れを発生させる
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);
}
}
}
}
![B7a8ec54 e28b 4e29 9187 6e596afda6cd](https://codegenius.org/uploads/slide/image/1712/b7a8ec54-e28b-4e29-9187-6e596afda6cd.jpeg)
![761317e7 c4e2 4695 80d7 042b5293878e](https://codegenius.org/uploads/slide/image/1710/761317e7-c4e2-4695-80d7-042b5293878e.jpeg)
![Ab8a62af e233 4381 b482 76684ae38e6b](https://codegenius.org/uploads/slide/image/1711/ab8a62af-e233-4381-b482-76684ae38e6b.jpeg)
弾切れを発生させる
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);
}
}
}
}
![B7a8ec54 e28b 4e29 9187 6e596afda6cd](https://codegenius.org/uploads/slide/image/1712/b7a8ec54-e28b-4e29-9187-6e596afda6cd.jpeg)
弾切れの発生①(一定時間連射で弾切れ)