体当たり攻撃でPlayerがダメージを受ける



体当たり攻撃
using System.Collections;
using System.Collections.Generic;
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") || 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);
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;
void Start()
{
animator = GetComponent<Animator>();
}
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);
Invoke("Restart", 0.5f);
}
void Restart()
{
SceneManager.LoadScene("Main");
}
}

体当たり攻撃でPlayerがダメージを受ける