弾切れの発生②(発射パワーを表示する)
![93dfffb7 3d7b 479e 820d 2a55309d3213](https://codegenius.org/uploads/slide/image/1713/93dfffb7-3d7b-479e-820d-2a55309d3213.jpeg)
![4fe5eb18 a1b6 46c0 98d6 0fd55f4f16e2](https://codegenius.org/uploads/slide/image/1714/4fe5eb18-a1b6-46c0-98d6-0fd55f4f16e2.jpeg)
パワー量の表示
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);
}
}
}
}
![E55e60b1 65bf 4fdd 8918 7b717facc510](https://codegenius.org/uploads/slide/image/1715/e55e60b1-65bf-4fdd-8918-7b717facc510.jpeg)
![93dfffb7 3d7b 479e 820d 2a55309d3213](https://codegenius.org/uploads/slide/image/1713/93dfffb7-3d7b-479e-820d-2a55309d3213.jpeg)
![4fe5eb18 a1b6 46c0 98d6 0fd55f4f16e2](https://codegenius.org/uploads/slide/image/1714/4fe5eb18-a1b6-46c0-98d6-0fd55f4f16e2.jpeg)
パワー量の表示
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);
}
}
}
}
![E55e60b1 65bf 4fdd 8918 7b717facc510](https://codegenius.org/uploads/slide/image/1715/e55e60b1-65bf-4fdd-8918-7b717facc510.jpeg)
弾切れの発生②(発射パワーを表示する)