敵を倒した時にエフェクトを出す








自動削除
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AutoDestroy : MonoBehaviour
{
// 先頭は必ず「public」にすること(重要)
public void SelfDestroy()
{
Destroy(this.gameObject);
}
}




エフェクトの発生
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwordAttack : MonoBehaviour
{
public AudioClip sound;
public GameObject attackPoint;
public GameObject attack_front;
public GameObject attack_back;
private RaycastHit2D hit2d;
private PlayerMovement pm;
private float num;
// ★追加(エフェクト)
public GameObject effectPrefab;
void Start()
{
pm = GetComponent<PlayerMovement>();
attackPoint.transform.position = attack_front.transform.position;
}
void Update()
{
num = pm.moveH;
if (num == 1)
{
attackPoint.transform.position = attack_front.transform.position;
}
else if (num == -1)
{
attackPoint.transform.position = attack_back.transform.position;
}
hit2d = Physics2D.Raycast(attackPoint.transform.position, Vector2.right * num, 0.3f);
Debug.DrawRay(attackPoint.transform.position, Vector2.right * num, Color.white, 0.3f);
}
public void Attack()
{
if (hit2d.collider != null)
{
if (hit2d.collider.CompareTag("Enemy"))
{
Destroy(hit2d.collider.gameObject);
AudioSource.PlayClipAtPoint(sound, transform.position);
// ★追加(エフェクト)
Instantiate(effectPrefab, hit2d.collider.transform.position, Quaternion.identity);
}
}
}
}


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







自動削除
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AutoDestroy : MonoBehaviour
{
// 先頭は必ず「public」にすること(重要)
public void SelfDestroy()
{
Destroy(this.gameObject);
}
}




エフェクトの発生
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwordAttack : MonoBehaviour
{
public AudioClip sound;
public GameObject attackPoint;
public GameObject attack_front;
public GameObject attack_back;
private RaycastHit2D hit2d;
private PlayerMovement pm;
private float num;
// ★追加(エフェクト)
public GameObject effectPrefab;
void Start()
{
pm = GetComponent<PlayerMovement>();
attackPoint.transform.position = attack_front.transform.position;
}
void Update()
{
num = pm.moveH;
if (num == 1)
{
attackPoint.transform.position = attack_front.transform.position;
}
else if (num == -1)
{
attackPoint.transform.position = attack_back.transform.position;
}
hit2d = Physics2D.Raycast(attackPoint.transform.position, Vector2.right * num, 0.3f);
Debug.DrawRay(attackPoint.transform.position, Vector2.right * num, Color.white, 0.3f);
}
public void Attack()
{
if (hit2d.collider != null)
{
if (hit2d.collider.CompareTag("Enemy"))
{
Destroy(hit2d.collider.gameObject);
AudioSource.PlayClipAtPoint(sound, transform.position);
// ★追加(エフェクト)
Instantiate(effectPrefab, hit2d.collider.transform.position, Quaternion.identity);
}
}
}
}


敵を倒した時にエフェクトを出す