(発展)タイムバトルの作成
TankHealthスクリプトの修正
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class TankHealth : MonoBehaviour
{
public GameObject effectPrefab1;
public GameObject effectPrefab2;
public int tankHP;
public Text HPLabel;
public Slider HPSlider;
// ★修正
//public GameObject winLabel;
// ★追加
public string winnerScene;
void Start()
{
HPLabel.text = "HP:" + tankHP;
HPSlider.value = tankHP;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "EnemyShell" || other.gameObject.tag == "Shell")
{
tankHP -= 1;
HPLabel.text = "HP:" + tankHP;
HPSlider.value = tankHP;
Destroy(other.gameObject);
if (tankHP > 0)
{
GameObject effect1 = Instantiate(effectPrefab1, transform.position, Quaternion.identity);
Destroy(effect1, 1.0f);
}
else
{
GameObject effect2 = Instantiate(effectPrefab2, transform.position, Quaternion.identity);
Destroy(effect2, 1.0f);
//Destroy (gameObject);
this.gameObject.SetActive(false);
// ★修正
//winLabel.SetActive (true);
// ★修正
Invoke("GoToEnding", 1.5f);
}
}
}
// ★修正
void GoToEnding()
{
SceneManager.LoadScene(winnerScene);
}
public void AddHP(int amount)
{
tankHP += amount;
if (tankHP > 5)
{
tankHP = 5;
}
HPLabel.text = "HP:" + tankHP;
HPSlider.value = tankHP;
}
}
GameControllerスクリプトの作成
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameController : MonoBehaviour
{
public Text timeLabel;
public float timeCount;
private int player1HP;
private int player2HP;
void Start()
{
timeLabel.text = "TIME:" + timeCount;
}
void Update()
{
timeCount -= Time.deltaTime;
timeLabel.text = "TIME:" + timeCount.ToString("0");
// ★ポイント
// プレーヤー1とプレーヤー2のHP情報を取得する。
if (GameObject.Find("Player1") != null)
{
player1HP = GameObject.Find("Player1").GetComponent<TankHealth>().tankHP;
}
if (GameObject.Find("Player2") != null)
{
player2HP = GameObject.Find("Player2").GetComponent<TankHealth>().tankHP;
}
// 残り時間が0になったら・・・・、
if (timeCount < 0)
{
// レーヤー1とプレーヤー2のHP情報を比較する。
if (player1HP > player2HP)
{
SceneManager.LoadScene("Player1Win");
}
else if (player1HP < player2HP)
{
SceneManager.LoadScene("Player2Win");
}
else
{
SceneManager.LoadScene("Draw");
}
}
}
}
TankHealthスクリプトの修正
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class TankHealth : MonoBehaviour
{
public GameObject effectPrefab1;
public GameObject effectPrefab2;
public int tankHP;
public Text HPLabel;
public Slider HPSlider;
// ★修正
//public GameObject winLabel;
// ★追加
public string winnerScene;
void Start()
{
HPLabel.text = "HP:" + tankHP;
HPSlider.value = tankHP;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "EnemyShell" || other.gameObject.tag == "Shell")
{
tankHP -= 1;
HPLabel.text = "HP:" + tankHP;
HPSlider.value = tankHP;
Destroy(other.gameObject);
if (tankHP > 0)
{
GameObject effect1 = Instantiate(effectPrefab1, transform.position, Quaternion.identity);
Destroy(effect1, 1.0f);
}
else
{
GameObject effect2 = Instantiate(effectPrefab2, transform.position, Quaternion.identity);
Destroy(effect2, 1.0f);
//Destroy (gameObject);
this.gameObject.SetActive(false);
// ★修正
//winLabel.SetActive (true);
// ★修正
Invoke("GoToEnding", 1.5f);
}
}
}
// ★修正
void GoToEnding()
{
SceneManager.LoadScene(winnerScene);
}
public void AddHP(int amount)
{
tankHP += amount;
if (tankHP > 5)
{
tankHP = 5;
}
HPLabel.text = "HP:" + tankHP;
HPSlider.value = tankHP;
}
}
GameControllerスクリプトの作成
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameController : MonoBehaviour
{
public Text timeLabel;
public float timeCount;
private int player1HP;
private int player2HP;
void Start()
{
timeLabel.text = "TIME:" + timeCount;
}
void Update()
{
timeCount -= Time.deltaTime;
timeLabel.text = "TIME:" + timeCount.ToString("0");
// ★ポイント
// プレーヤー1とプレーヤー2のHP情報を取得する。
if (GameObject.Find("Player1") != null)
{
player1HP = GameObject.Find("Player1").GetComponent<TankHealth>().tankHP;
}
if (GameObject.Find("Player2") != null)
{
player2HP = GameObject.Find("Player2").GetComponent<TankHealth>().tankHP;
}
// 残り時間が0になったら・・・・、
if (timeCount < 0)
{
// レーヤー1とプレーヤー2のHP情報を比較する。
if (player1HP > player2HP)
{
SceneManager.LoadScene("Player1Win");
}
else if (player1HP < player2HP)
{
SceneManager.LoadScene("Player2Win");
}
else
{
SceneManager.LoadScene("Draw");
}
}
}
}
(発展)タイムバトルの作成