Playerに耐久力をつける


耐久力の追加
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerHP : MonoBehaviour
{
private Animator animator;
public AudioClip sound;
// ★追加(耐久力)
private int maxHP = 5; // HP数は自由
private int HP;
public GameObject[] hpIcons;
void Start()
{
animator = GetComponent<Animator>();
// ★追加(耐久力)
HP = maxHP;
}
// ★追加(耐久力)
private void Update()
{
for (int i = 0; i < hpIcons.Length; i++)
{
if (i < HP)
{
hpIcons[i].SetActive(true);
}
else
{
hpIcons[i].SetActive(false);
}
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Enemy") || collision.gameObject.CompareTag("Burning"))
{
Damage();
}
else if (collision.gameObject.CompareTag("FireBall"))
{
Destroy(collision.gameObject);
Damage();
}
}
void Damage()
{
animator.SetTrigger("Damage");
AudioSource.PlayClipAtPoint(sound, transform.position);
// ★追加(耐久力)
HP -= 1;
// ★改良(耐久力)
if (HP < 1)
{
Invoke("Restart", 0.5f);
}
}
void Restart()
{
SceneManager.LoadScene("Main");
}
}


【2022版】DarkCastle(全39回)
他のコースを見る

耐久力の追加
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerHP : MonoBehaviour
{
private Animator animator;
public AudioClip sound;
// ★追加(耐久力)
private int maxHP = 5; // HP数は自由
private int HP;
public GameObject[] hpIcons;
void Start()
{
animator = GetComponent<Animator>();
// ★追加(耐久力)
HP = maxHP;
}
// ★追加(耐久力)
private void Update()
{
for (int i = 0; i < hpIcons.Length; i++)
{
if (i < HP)
{
hpIcons[i].SetActive(true);
}
else
{
hpIcons[i].SetActive(false);
}
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Enemy") || collision.gameObject.CompareTag("Burning"))
{
Damage();
}
else if (collision.gameObject.CompareTag("FireBall"))
{
Destroy(collision.gameObject);
Damage();
}
}
void Damage()
{
animator.SetTrigger("Damage");
AudioSource.PlayClipAtPoint(sound, transform.position);
// ★追加(耐久力)
HP -= 1;
// ★改良(耐久力)
if (HP < 1)
{
Invoke("Restart", 0.5f);
}
}
void Restart()
{
SceneManager.LoadScene("Main");
}
}


Playerに耐久力をつける