プレーヤーのステータス①(HPを設定する)
![Dc0eb06c de66 4caa 90e9 ba4564289b31](https://codegenius.org/uploads/slide/image/1269/dc0eb06c-de66-4caa-90e9-ba4564289b31.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);
}
}
}
}
![F85d20ea 33b5 4ef3 baee 3d1f192c40bd](https://codegenius.org/uploads/slide/image/1270/f85d20ea-33b5-4ef3-baee-3d1f192c40bd.jpeg)
![275c682e b191 4f52 8173 730a2ef09e94](https://codegenius.org/uploads/slide/image/1271/275c682e-b191-4f52-8173-730a2ef09e94.jpeg)
![C8147472 2a40 4bc0 b2d3 7a09fbaa85b2](https://codegenius.org/uploads/slide/image/1272/c8147472-2a40-4bc0-b2d3-7a09fbaa85b2.jpeg)
![F9e84553 cc6f 4955 84a4 05fc7cd76347](https://codegenius.org/uploads/slide/image/1273/f9e84553-cc6f-4955-84a4-05fc7cd76347.jpeg)
Danmaku I(基礎1/全22回)
他のコースを見る![Dc0eb06c de66 4caa 90e9 ba4564289b31](https://codegenius.org/uploads/slide/image/1269/dc0eb06c-de66-4caa-90e9-ba4564289b31.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);
}
}
}
}
![F85d20ea 33b5 4ef3 baee 3d1f192c40bd](https://codegenius.org/uploads/slide/image/1270/f85d20ea-33b5-4ef3-baee-3d1f192c40bd.jpeg)
![275c682e b191 4f52 8173 730a2ef09e94](https://codegenius.org/uploads/slide/image/1271/275c682e-b191-4f52-8173-730a2ef09e94.jpeg)
![C8147472 2a40 4bc0 b2d3 7a09fbaa85b2](https://codegenius.org/uploads/slide/image/1272/c8147472-2a40-4bc0-b2d3-7a09fbaa85b2.jpeg)
![F9e84553 cc6f 4955 84a4 05fc7cd76347](https://codegenius.org/uploads/slide/image/1273/f9e84553-cc6f-4955-84a4-05fc7cd76347.jpeg)
プレーヤーのステータス①(HPを設定する)