プレーヤーがダメージを受ける




プレーヤーがダメージを受ける
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerHP : MonoBehaviour
{
private Animator animator;
public AudioClip sound;
void Start()
{
animator = GetComponent<Animator>();
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Enemy"))
{
animator.SetTrigger("Damage");
AudioSource.PlayClipAtPoint(sound, transform.position);
}
}
}




リスタート
using UnityEngine;
// ★追加(リスタート)
using UnityEngine.SceneManagement;
public class PlayerHP : MonoBehaviour
{
private Animator animator;
public AudioClip sound;
void Start()
{
animator = GetComponent<Animator>();
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.CompareTag("Enemy"))
{
animator.SetTrigger("Damage");
AudioSource.PlayClipAtPoint(sound, transform.position);
// ★追加(リスタート)
Invoke("Restart", 0.5f);
}
}
// ★追加(リスタート)
void Restart()
{
SceneManager.LoadScene("Main");
}
}

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



プレーヤーがダメージを受ける
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerHP : MonoBehaviour
{
private Animator animator;
public AudioClip sound;
void Start()
{
animator = GetComponent<Animator>();
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Enemy"))
{
animator.SetTrigger("Damage");
AudioSource.PlayClipAtPoint(sound, transform.position);
}
}
}




リスタート
using UnityEngine;
// ★追加(リスタート)
using UnityEngine.SceneManagement;
public class PlayerHP : MonoBehaviour
{
private Animator animator;
public AudioClip sound;
void Start()
{
animator = GetComponent<Animator>();
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.CompareTag("Enemy"))
{
animator.SetTrigger("Damage");
AudioSource.PlayClipAtPoint(sound, transform.position);
// ★追加(リスタート)
Invoke("Restart", 0.5f);
}
}
// ★追加(リスタート)
void Restart()
{
SceneManager.LoadScene("Main");
}
}

プレーヤーがダメージを受ける