ミサイルの発射方法の変更
ミサイル自動発射
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 = 300;
private int shotPower;
private Slider powerSlider;
const int RecoveryTime = 10;
private int counter;
private Slider waitTimeSlider;
void Start(){
shotPower = maxPower;
powerSlider = GameObject.Find ("PowerSlider").GetComponent<Slider> ();
powerSlider.maxValue = maxPower;
powerSlider.value = shotPower;
waitTimeSlider = GameObject.Find ("WaitTimeSlider").GetComponent<Slider> ();
waitTimeSlider.maxValue = RecoveryTime;
waitTimeSlider.value = RecoveryTime;
}
// ★★修正
// どのような仕組みで「手動発射→自動発射」に変更できるのか確認してください。
void Update () {
if (shotPower <= 0 && counter <= 0) {
StartCoroutine (RecoverPower());
}
timeCount += 1;
if (shotPower <= 0)
{
return;
}
shotPower -= 1;
powerSlider.value = shotPower;
if (timeCount % 5 == 0)
{
GameObject missile = Instantiate(missilePrefab, transform.position, Quaternion.identity) as GameObject;
Rigidbody missileRb = missile.GetComponent<Rigidbody>();
missileRb.AddForce(transform.forward * missileSpeed);
AudioSource.PlayClipAtPoint(fireSound, transform.position);
Destroy(missile, 2.0f);
}
}
IEnumerator RecoverPower(){
counter = RecoveryTime;
while (counter > 0) {
yield return new WaitForSeconds (1.0f);
counter -= 1;
waitTimeSlider.value = counter;
}
shotPower = maxPower;
powerSlider.value = shotPower;
waitTimeSlider.value = RecoveryTime;
}
public void ResetPower(){
shotPower = maxPower;
powerSlider.value = shotPower;
counter = 0;
waitTimeSlider.value = RecoveryTime;
}
}
ミサイル自動発射
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 = 300;
private int shotPower;
private Slider powerSlider;
const int RecoveryTime = 10;
private int counter;
private Slider waitTimeSlider;
void Start(){
shotPower = maxPower;
powerSlider = GameObject.Find ("PowerSlider").GetComponent<Slider> ();
powerSlider.maxValue = maxPower;
powerSlider.value = shotPower;
waitTimeSlider = GameObject.Find ("WaitTimeSlider").GetComponent<Slider> ();
waitTimeSlider.maxValue = RecoveryTime;
waitTimeSlider.value = RecoveryTime;
}
// ★★修正
// どのような仕組みで「手動発射→自動発射」に変更できるのか確認してください。
void Update () {
if (shotPower <= 0 && counter <= 0) {
StartCoroutine (RecoverPower());
}
timeCount += 1;
if (shotPower <= 0)
{
return;
}
shotPower -= 1;
powerSlider.value = shotPower;
if (timeCount % 5 == 0)
{
GameObject missile = Instantiate(missilePrefab, transform.position, Quaternion.identity) as GameObject;
Rigidbody missileRb = missile.GetComponent<Rigidbody>();
missileRb.AddForce(transform.forward * missileSpeed);
AudioSource.PlayClipAtPoint(fireSound, transform.position);
Destroy(missile, 2.0f);
}
}
IEnumerator RecoverPower(){
counter = RecoveryTime;
while (counter > 0) {
yield return new WaitForSeconds (1.0f);
counter -= 1;
waitTimeSlider.value = counter;
}
shotPower = maxPower;
powerSlider.value = shotPower;
waitTimeSlider.value = RecoveryTime;
}
public void ResetPower(){
shotPower = maxPower;
powerSlider.value = shotPower;
counter = 0;
waitTimeSlider.value = RecoveryTime;
}
}
ミサイルの発射方法の変更