プレーヤーのステータス①(HPを設定する)
![15df07da 9998 4dc0 8983 7e3cd2f72ee8](https://codegenius.org/uploads/slide/image/7550/15df07da-9998-4dc0-8983-7e3cd2f72ee8.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 maxHP;
private int HP;
void Start()
{
HP = maxHP;
}
private void OnTriggerEnter(Collider other)
{
if(other.CompareTag("EnemyMissile"))
{
HP -= 1;
AudioSource.PlayClipAtPoint(damageSound, Camera.main.transform.position);
Destroy(other.gameObject);
if(HP == 0)
{
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 1.0f);
AudioSource.PlayClipAtPoint(destroySound, Camera.main.transform.position);
// Playerは非アクティブ状態にする(ポイント)
this.gameObject.SetActive(false);
}
}
}
}
![32e1fd17 fdd6 4ce7 9f9e 733de7ea7c2d](https://codegenius.org/uploads/slide/image/7551/32e1fd17-fdd6-4ce7-9f9e-733de7ea7c2d.jpeg)
![E486a80c b860 471c 91d5 7da8b91a118f](https://codegenius.org/uploads/slide/image/7552/e486a80c-b860-471c-91d5-7da8b91a118f.jpeg)
![F6b6ce98 d2cf 48a1 a880 3e7e2bcdaffe](https://codegenius.org/uploads/slide/image/7553/f6b6ce98-d2cf-48a1-a880-3e7e2bcdaffe.jpeg)
![83817b32 0ee9 45e2 8a01 abe39b194499](https://codegenius.org/uploads/slide/image/7554/83817b32-0ee9-45e2-8a01-abe39b194499.jpeg)
【2021版】Danmaku(基礎/全55回)
他のコースを見る![15df07da 9998 4dc0 8983 7e3cd2f72ee8](https://codegenius.org/uploads/slide/image/7550/15df07da-9998-4dc0-8983-7e3cd2f72ee8.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 maxHP;
private int HP;
void Start()
{
HP = maxHP;
}
private void OnTriggerEnter(Collider other)
{
if(other.CompareTag("EnemyMissile"))
{
HP -= 1;
AudioSource.PlayClipAtPoint(damageSound, Camera.main.transform.position);
Destroy(other.gameObject);
if(HP == 0)
{
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 1.0f);
AudioSource.PlayClipAtPoint(destroySound, Camera.main.transform.position);
// Playerは非アクティブ状態にする(ポイント)
this.gameObject.SetActive(false);
}
}
}
}
![32e1fd17 fdd6 4ce7 9f9e 733de7ea7c2d](https://codegenius.org/uploads/slide/image/7551/32e1fd17-fdd6-4ce7-9f9e-733de7ea7c2d.jpeg)
![E486a80c b860 471c 91d5 7da8b91a118f](https://codegenius.org/uploads/slide/image/7552/e486a80c-b860-471c-91d5-7da8b91a118f.jpeg)
![F6b6ce98 d2cf 48a1 a880 3e7e2bcdaffe](https://codegenius.org/uploads/slide/image/7553/f6b6ce98-d2cf-48a1-a880-3e7e2bcdaffe.jpeg)
![83817b32 0ee9 45e2 8a01 abe39b194499](https://codegenius.org/uploads/slide/image/7554/83817b32-0ee9-45e2-8a01-abe39b194499.jpeg)
プレーヤーのステータス①(HPを設定する)