トラップの作成3(Playerに耐久力を追加する)


HPの追加
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerHP : MonoBehaviour
{
public AudioClip damageSound;
private Animator animator;
// ★追加(HP)
private int HP;
private int maxHP = 3;
public GameObject[] hpIcons;
private void Start()
{
animator = GetComponent<Animator>();
// ★追加(HP)
HP = maxHP;
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Trap"))
{
// ★追加(HP)
HP -= 1;
HPIconManager();
animator.SetTrigger("Damage");
AudioSource.PlayClipAtPoint(damageSound, transform.position);
}
}
// ★追加(HP)
// HPアイコンの表示を管理するメソッド
void HPIconManager()
{
for (int i = 0; i < hpIcons.Length; i++)
{
if (i < HP)
{
hpIcons[i].SetActive(true);
}
else
{
hpIcons[i].SetActive(false);
}
}
}
}



リスタート
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★追加(リスタート)
using UnityEngine.SceneManagement;
public class PlayerHP : MonoBehaviour
{
public AudioClip damageSound;
private Animator animator;
private int HP;
private int maxHP = 3;
public GameObject[] hpIcons;
// ★追加(リスタート)
public AudioClip dieSound;
private void Start()
{
animator = GetComponent<Animator>();
HP = maxHP;
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Trap"))
{
HP -= 1;
HPIconManager();
animator.SetTrigger("Damage");
// ★追加&改良(リスタート)
if (HP > 0)
{
AudioSource.PlayClipAtPoint(damageSound, transform.position);
}
else
{
AudioSource.PlayClipAtPoint(dieSound, transform.position);
// 0.15秒後にリスタートにする。
Invoke("Restart", 0.15f);
}
}
}
void HPIconManager()
{
for (int i = 0; i < hpIcons.Length; i++)
{
if (i < HP)
{
hpIcons[i].SetActive(true);
}
else
{
hpIcons[i].SetActive(false);
}
}
}
// ★追加(リスタート)
void Restart()
{
SceneManager.LoadScene("Main");
}
}



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

HPの追加
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerHP : MonoBehaviour
{
public AudioClip damageSound;
private Animator animator;
// ★追加(HP)
private int HP;
private int maxHP = 3;
public GameObject[] hpIcons;
private void Start()
{
animator = GetComponent<Animator>();
// ★追加(HP)
HP = maxHP;
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Trap"))
{
// ★追加(HP)
HP -= 1;
HPIconManager();
animator.SetTrigger("Damage");
AudioSource.PlayClipAtPoint(damageSound, transform.position);
}
}
// ★追加(HP)
// HPアイコンの表示を管理するメソッド
void HPIconManager()
{
for (int i = 0; i < hpIcons.Length; i++)
{
if (i < HP)
{
hpIcons[i].SetActive(true);
}
else
{
hpIcons[i].SetActive(false);
}
}
}
}



リスタート
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★追加(リスタート)
using UnityEngine.SceneManagement;
public class PlayerHP : MonoBehaviour
{
public AudioClip damageSound;
private Animator animator;
private int HP;
private int maxHP = 3;
public GameObject[] hpIcons;
// ★追加(リスタート)
public AudioClip dieSound;
private void Start()
{
animator = GetComponent<Animator>();
HP = maxHP;
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Trap"))
{
HP -= 1;
HPIconManager();
animator.SetTrigger("Damage");
// ★追加&改良(リスタート)
if (HP > 0)
{
AudioSource.PlayClipAtPoint(damageSound, transform.position);
}
else
{
AudioSource.PlayClipAtPoint(dieSound, transform.position);
// 0.15秒後にリスタートにする。
Invoke("Restart", 0.15f);
}
}
}
void HPIconManager()
{
for (int i = 0; i < hpIcons.Length; i++)
{
if (i < HP)
{
hpIcons[i].SetActive(true);
}
else
{
hpIcons[i].SetActive(false);
}
}
}
// ★追加(リスタート)
void Restart()
{
SceneManager.LoadScene("Main");
}
}



トラップの作成3(Playerに耐久力を追加する)