弾切れの発生②(ショットパワーを表示する)
ショットパワー残量の表示
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;
// ★追加(パワー残量の表示)
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);
}
}
}
}
【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;
// ★追加(パワー残量の表示)
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);
}
}
}
}
弾切れの発生②(ショットパワーを表示する)