プレーヤーのステータス①(HPを設定する)
![E32dd935 ee57 4cdd ac08 e6d298380909](https://codegenius.org/uploads/slide/image/4594/e32dd935-ee57-4cdd-ac08-e6d298380909.jpeg)
プレーヤーにHPを設定する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerHealth : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip damageSound;
public AudioClip destroySound;
public int playerHP;
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.CompareTag("EnemyMissile"))
{
playerHP -= 1;
AudioSource.PlayClipAtPoint(damageSound, Camera.main.transform.position);
Destroy(other.gameObject);
if(playerHP == 0)
{
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 1.0f);
AudioSource.PlayClipAtPoint(destroySound, Camera.main.transform.position);
// プレーヤーを破壊するのではなく、非アクティブ状態にする(ポイント)
this.gameObject.SetActive(false);
}
}
}
}
![311389c9 8fc2 4449 92df 9e1accd3f192](https://codegenius.org/uploads/slide/image/4595/311389c9-8fc2-4449-92df-9e1accd3f192.jpeg)
![C89da39f 5afb 4c58 beb7 098737eb7e92](https://codegenius.org/uploads/slide/image/4596/c89da39f-5afb-4c58-beb7-098737eb7e92.jpeg)
![Eefdc5ed 2c10 4b5f a6da 86c4ba116806](https://codegenius.org/uploads/slide/image/4597/eefdc5ed-2c10-4b5f-a6da-86c4ba116806.jpeg)
![5153a908 0e8e 410f 9b0a b0d91da336e4](https://codegenius.org/uploads/slide/image/4598/5153a908-0e8e-410f-9b0a-b0d91da336e4.jpeg)
【2019版】Danmaku I(基礎1/全22回)
他のコースを見る![E32dd935 ee57 4cdd ac08 e6d298380909](https://codegenius.org/uploads/slide/image/4594/e32dd935-ee57-4cdd-ac08-e6d298380909.jpeg)
プレーヤーにHPを設定する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerHealth : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip damageSound;
public AudioClip destroySound;
public int playerHP;
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.CompareTag("EnemyMissile"))
{
playerHP -= 1;
AudioSource.PlayClipAtPoint(damageSound, Camera.main.transform.position);
Destroy(other.gameObject);
if(playerHP == 0)
{
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 1.0f);
AudioSource.PlayClipAtPoint(destroySound, Camera.main.transform.position);
// プレーヤーを破壊するのではなく、非アクティブ状態にする(ポイント)
this.gameObject.SetActive(false);
}
}
}
}
![311389c9 8fc2 4449 92df 9e1accd3f192](https://codegenius.org/uploads/slide/image/4595/311389c9-8fc2-4449-92df-9e1accd3f192.jpeg)
![C89da39f 5afb 4c58 beb7 098737eb7e92](https://codegenius.org/uploads/slide/image/4596/c89da39f-5afb-4c58-beb7-098737eb7e92.jpeg)
![Eefdc5ed 2c10 4b5f a6da 86c4ba116806](https://codegenius.org/uploads/slide/image/4597/eefdc5ed-2c10-4b5f-a6da-86c4ba116806.jpeg)
![5153a908 0e8e 410f 9b0a b0d91da336e4](https://codegenius.org/uploads/slide/image/4598/5153a908-0e8e-410f-9b0a-b0d91da336e4.jpeg)
プレーヤーのステータス①(HPを設定する)