HP回復アイテムの作成
![2efbb20f 6143 44e1 9e1d 32392faee07e](https://codegenius.org/uploads/slide/image/2812/2efbb20f-6143-44e1-9e1d-32392faee07e.jpeg)
![Bffd28ef b074 482e b34c b7588eea1317](https://codegenius.org/uploads/slide/image/2813/bffd28ef-b074-482e-b34c-b7588eea1317.jpeg)
PlayerHPの改良
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayerHP : MonoBehaviour {
public int HP = 30;
public Slider hpSlider;
public AudioClip soundA;
public AudioClip soundB;
void Start () {
hpSlider.value = HP;
}
void OnCollisionEnter(Collision other){
if (other.gameObject.tag == "EnemyBullet") {
Destroy (other.gameObject);
if (HP > 0) {
AudioSource.PlayClipAtPoint (soundA, Camera.main.transform.position);
} else {
AudioSource.PlayClipAtPoint (soundB, Camera.main.transform.position);
Invoke ("Retry", 1.0f);
return;
}
HP -= 1;
hpSlider.value = HP;
}
}
void Retry(){
SceneManager.LoadScene ("Main");
}
// ★追加
public void AddHP(int amount){
HP += amount;
if (HP > 30) {
HP = 30;
}
hpSlider.value = HP;
}
}
![8841cdad d410 4fda 9753 3f42dbadba60](https://codegenius.org/uploads/slide/image/2814/8841cdad-d410-4fda-9753-3f42dbadba60.jpeg)
HPItem
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HPItem : MonoBehaviour {
public AudioClip getSound;
private PlayerHP pHP;
void Start(){
pHP = GameObject.Find ("PlayerBody").GetComponent<PlayerHP> ();
}
void OnTriggerEnter(Collider other){
if (other.gameObject.tag == "Player") {
Destroy (gameObject);
AudioSource.PlayClipAtPoint (getSound, Camera.main.transform.position);
pHP.AddHP (5);
}
}
}
EscapeCombat(メモ)
他のコースを見る![2efbb20f 6143 44e1 9e1d 32392faee07e](https://codegenius.org/uploads/slide/image/2812/2efbb20f-6143-44e1-9e1d-32392faee07e.jpeg)
![Bffd28ef b074 482e b34c b7588eea1317](https://codegenius.org/uploads/slide/image/2813/bffd28ef-b074-482e-b34c-b7588eea1317.jpeg)
PlayerHPの改良
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayerHP : MonoBehaviour {
public int HP = 30;
public Slider hpSlider;
public AudioClip soundA;
public AudioClip soundB;
void Start () {
hpSlider.value = HP;
}
void OnCollisionEnter(Collision other){
if (other.gameObject.tag == "EnemyBullet") {
Destroy (other.gameObject);
if (HP > 0) {
AudioSource.PlayClipAtPoint (soundA, Camera.main.transform.position);
} else {
AudioSource.PlayClipAtPoint (soundB, Camera.main.transform.position);
Invoke ("Retry", 1.0f);
return;
}
HP -= 1;
hpSlider.value = HP;
}
}
void Retry(){
SceneManager.LoadScene ("Main");
}
// ★追加
public void AddHP(int amount){
HP += amount;
if (HP > 30) {
HP = 30;
}
hpSlider.value = HP;
}
}
![8841cdad d410 4fda 9753 3f42dbadba60](https://codegenius.org/uploads/slide/image/2814/8841cdad-d410-4fda-9753-3f42dbadba60.jpeg)
HPItem
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HPItem : MonoBehaviour {
public AudioClip getSound;
private PlayerHP pHP;
void Start(){
pHP = GameObject.Find ("PlayerBody").GetComponent<PlayerHP> ();
}
void OnTriggerEnter(Collider other){
if (other.gameObject.tag == "Player") {
Destroy (gameObject);
AudioSource.PlayClipAtPoint (getSound, Camera.main.transform.position);
pHP.AddHP (5);
}
}
}
HP回復アイテムの作成