回復アイテムを取得するとプレーヤーの視界がクリアーになるようにする


プレーヤーの視界を回復させる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerHealth : MonoBehaviour {
public int playerHP;
public AudioClip damageSound;
private Image damageScreenImage;
private Color color;
void Start(){
damageScreenImage = GameObject.Find("DamageScreen").GetComponent<Image>();
color = damageScreenImage.color;
color.a = 0.0f;
damageScreenImage.color = color;
}
void OnCollisionEnter(Collision other){
if(other.gameObject.tag == "EnemyBullet"){
playerHP -= 1;
Destroy(other.gameObject);
AudioSource.PlayClipAtPoint(damageSound, Camera.main.transform.position);
color.a += 0.1f;
damageScreenImage.color = color;
}
}
public void AddHP(int amount){
playerHP += amount;
if(playerHP > 10){
playerHP = 10;
}
// ★★プレーヤーの視界不良を回復させる。
// a(アルファ値)のデータは「小数」なので、データの型をfloat型に変換する(重要ポイント)
float amountF = (float)amount;
// HP回復量を最大HPで割る(例;HP回復5 / 最大HP10 = 0.5)
color.a -= (amountF / 10);
// a(アルファ値)の最小値は「0」なので、それ未満にならないようにする。
if(color.a < 0){
color.a = 0;
}
damageScreenImage.color = color;
}
}


EscapeCombat
他のコースを見る

プレーヤーの視界を回復させる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerHealth : MonoBehaviour {
public int playerHP;
public AudioClip damageSound;
private Image damageScreenImage;
private Color color;
void Start(){
damageScreenImage = GameObject.Find("DamageScreen").GetComponent<Image>();
color = damageScreenImage.color;
color.a = 0.0f;
damageScreenImage.color = color;
}
void OnCollisionEnter(Collision other){
if(other.gameObject.tag == "EnemyBullet"){
playerHP -= 1;
Destroy(other.gameObject);
AudioSource.PlayClipAtPoint(damageSound, Camera.main.transform.position);
color.a += 0.1f;
damageScreenImage.color = color;
}
}
public void AddHP(int amount){
playerHP += amount;
if(playerHP > 10){
playerHP = 10;
}
// ★★プレーヤーの視界不良を回復させる。
// a(アルファ値)のデータは「小数」なので、データの型をfloat型に変換する(重要ポイント)
float amountF = (float)amount;
// HP回復量を最大HPで割る(例;HP回復5 / 最大HP10 = 0.5)
color.a -= (amountF / 10);
// a(アルファ値)の最小値は「0」なので、それ未満にならないようにする。
if(color.a < 0){
color.a = 0;
}
damageScreenImage.color = color;
}
}


回復アイテムを取得するとプレーヤーの視界がクリアーになるようにする