弾切れの発生③(ショットパワーを回復させる)
ショットパワーを回復させる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FireMissile : MonoBehaviour
{
public GameObject missilePrefab;
public float missileSpeed;
public AudioClip fireSound;
private int timeCount;
private int maxPower = 100;
private int shotPower;
public Slider powerSlider;
private void Start()
{
shotPower = maxPower;
powerSlider.maxValue = maxPower;
powerSlider.value = shotPower;
}
void Update()
{
timeCount += 1;
if (Input.GetButton("Jump"))
{
if(shotPower <= 0)
{
return;
}
shotPower -= 1;
// ★追加(ショットパワーの回復)
// このコードの意味を考えよう!
if(shotPower < 0)
{
shotPower = 0;
}
powerSlider.value = shotPower;
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);
}
}
// ★追加(ショットパワーの回復)
else
{
shotPower += 1;
if(shotPower > maxPower)
{
shotPower = maxPower;
}
powerSlider.value = shotPower;
}
}
}
【2019版】Danmaku Ⅱ(基礎2/全38回)
他のコースを見るショットパワーを回復させる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FireMissile : MonoBehaviour
{
public GameObject missilePrefab;
public float missileSpeed;
public AudioClip fireSound;
private int timeCount;
private int maxPower = 100;
private int shotPower;
public Slider powerSlider;
private void Start()
{
shotPower = maxPower;
powerSlider.maxValue = maxPower;
powerSlider.value = shotPower;
}
void Update()
{
timeCount += 1;
if (Input.GetButton("Jump"))
{
if(shotPower <= 0)
{
return;
}
shotPower -= 1;
// ★追加(ショットパワーの回復)
// このコードの意味を考えよう!
if(shotPower < 0)
{
shotPower = 0;
}
powerSlider.value = shotPower;
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);
}
}
// ★追加(ショットパワーの回復)
else
{
shotPower += 1;
if(shotPower > maxPower)
{
shotPower = maxPower;
}
powerSlider.value = shotPower;
}
}
}
弾切れの発生③(ショットパワーを回復させる)