HP回復アイテムの作成


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;
}
}

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(メモ)
他のコースを見る

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;
}
}

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回復アイテムの作成