プレーヤーのHPを回復させるアイテムの作成
![88d29ef4 0ee0 46bc 984c ea9af97c165b](https://codegenius.org/uploads/slide/image/2149/88d29ef4-0ee0-46bc-984c-ea9af97c165b.jpeg)
![9d458b5b 2df4 4fdf a3e2 ce711e51fe12](https://codegenius.org/uploads/slide/image/2150/9d458b5b-2df4-4fdf-a3e2-ce711e51fe12.jpeg)
HPを増加させるメソッド
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;
void Start()
{
HPLabel.text = "HP:" + tankHP;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "EnemyShell")
{
tankHP -= 1;
HPLabel.text = "HP:" + 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);
this.gameObject.SetActive(false);
Invoke("GoToGameOver", 1.5f);
}
}
}
void GoToGameOver()
{
SceneManager.LoadScene("GameOver");
}
// ★追加
// publicをつけること(重要ポイント)
public void AddHP(int amount)
{
tankHP += amount;
// ここは何をコントロールしている考えてみましょう!
if (tankHP > 10)
{
tankHP = 10;
}
HPLabel.text = "HP:" + tankHP;
}
}
![17d8c3e9 6ddf 4875 9a47 31323afbbe2a](https://codegenius.org/uploads/slide/image/2151/17d8c3e9-6ddf-4875-9a47-31323afbbe2a.jpeg)
HPを回復させるアイテム
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HPItem : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip getSound;
private TankHealth th;
private int reward = 3; // いくつ回復させるかは自由!
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
// Find()メソッドの使い方をマスターすること
th = GameObject.Find("Tank").GetComponent<TankHealth>();
// AddHP()メソッドを呼び出して「引数」にrewardを与えている。
th.AddHP(reward);
Destroy(gameObject);
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
AudioSource.PlayClipAtPoint(getSound, Camera.main.transform.position);
}
}
}
![705a9d8c a383 4f32 bfb2 4b7ffdba242c](https://codegenius.org/uploads/slide/image/2152/705a9d8c-a383-4f32-bfb2-4b7ffdba242c.jpeg)
![Fc5b6a20 1d7c 4e2e 96af 7509da5ee182](https://codegenius.org/uploads/slide/image/2153/fc5b6a20-1d7c-4e2e-96af-7509da5ee182.jpeg)
BattleTank(基礎/全31回)
他のコースを見る![88d29ef4 0ee0 46bc 984c ea9af97c165b](https://codegenius.org/uploads/slide/image/2149/88d29ef4-0ee0-46bc-984c-ea9af97c165b.jpeg)
![9d458b5b 2df4 4fdf a3e2 ce711e51fe12](https://codegenius.org/uploads/slide/image/2150/9d458b5b-2df4-4fdf-a3e2-ce711e51fe12.jpeg)
HPを増加させるメソッド
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;
void Start()
{
HPLabel.text = "HP:" + tankHP;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "EnemyShell")
{
tankHP -= 1;
HPLabel.text = "HP:" + 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);
this.gameObject.SetActive(false);
Invoke("GoToGameOver", 1.5f);
}
}
}
void GoToGameOver()
{
SceneManager.LoadScene("GameOver");
}
// ★追加
// publicをつけること(重要ポイント)
public void AddHP(int amount)
{
tankHP += amount;
// ここは何をコントロールしている考えてみましょう!
if (tankHP > 10)
{
tankHP = 10;
}
HPLabel.text = "HP:" + tankHP;
}
}
![17d8c3e9 6ddf 4875 9a47 31323afbbe2a](https://codegenius.org/uploads/slide/image/2151/17d8c3e9-6ddf-4875-9a47-31323afbbe2a.jpeg)
HPを回復させるアイテム
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HPItem : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip getSound;
private TankHealth th;
private int reward = 3; // いくつ回復させるかは自由!
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
// Find()メソッドの使い方をマスターすること
th = GameObject.Find("Tank").GetComponent<TankHealth>();
// AddHP()メソッドを呼び出して「引数」にrewardを与えている。
th.AddHP(reward);
Destroy(gameObject);
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
AudioSource.PlayClipAtPoint(getSound, Camera.main.transform.position);
}
}
}
![705a9d8c a383 4f32 bfb2 4b7ffdba242c](https://codegenius.org/uploads/slide/image/2152/705a9d8c-a383-4f32-bfb2-4b7ffdba242c.jpeg)
![Fc5b6a20 1d7c 4e2e 96af 7509da5ee182](https://codegenius.org/uploads/slide/image/2153/fc5b6a20-1d7c-4e2e-96af-7509da5ee182.jpeg)
プレーヤーのHPを回復させるアイテムの作成