弾切れの発生②(発射パワーを表示する)
パワー量の表示
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;
// ★追加(パワー量の表示)
private Slider powerSlider;
void Start()
{
shotPower = maxPower;
// ★追加(パワー量の表示)
powerSlider = GameObject.Find("PowerSlider").GetComponent<Slider>();
powerSlider.maxValue = maxPower;
powerSlider.value = shotPower;
}
void Update()
{
timeCount += 1;
if (Input.GetButton("Fire1"))
{
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);
}
}
}
}
パワー量の表示
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;
// ★追加(パワー量の表示)
private Slider powerSlider;
void Start()
{
shotPower = maxPower;
// ★追加(パワー量の表示)
powerSlider = GameObject.Find("PowerSlider").GetComponent<Slider>();
powerSlider.maxValue = maxPower;
powerSlider.value = shotPower;
}
void Update()
{
timeCount += 1;
if (Input.GetButton("Fire1"))
{
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);
}
}
}
}
弾切れの発生②(発射パワーを表示する)