剣攻撃は有効だが、ソニックビームは無効にする



剣攻撃有効
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") || hit2d.collider.CompareTag("Wizard")) // ★追加(剣攻撃有効)
{
Destroy(hit2d.collider.gameObject);
AudioSource.PlayClipAtPoint(sound, transform.position);
Instantiate(effectPrefab, hit2d.collider.transform.position, Quaternion.identity);
pm.AddSonicCount(1);
}
}
}
}


ソニックビーム無効
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SonicBeam : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip sound;
// ★追加(ソニックビーム無効)
public GameObject noDamageEffectPrefab;
public AudioClip noDamageSound;
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.CompareTag("Enemy"))
{
Destroy(this.gameObject);
Destroy(collision.gameObject);
Instantiate(effectPrefab, collision.transform.position, Quaternion.identity);
AudioSource.PlayClipAtPoint(sound, transform.position);
}
// ★追加(ソニックビーム無効)
else if(collision.CompareTag("Wizard"))
{
Destroy(this.gameObject);
Instantiate(noDamageEffectPrefab, transform.position, Quaternion.identity);
AudioSource.PlayClipAtPoint(noDamageSound, transform.position);
}
}
}


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


剣攻撃有効
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") || hit2d.collider.CompareTag("Wizard")) // ★追加(剣攻撃有効)
{
Destroy(hit2d.collider.gameObject);
AudioSource.PlayClipAtPoint(sound, transform.position);
Instantiate(effectPrefab, hit2d.collider.transform.position, Quaternion.identity);
pm.AddSonicCount(1);
}
}
}
}


ソニックビーム無効
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SonicBeam : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip sound;
// ★追加(ソニックビーム無効)
public GameObject noDamageEffectPrefab;
public AudioClip noDamageSound;
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.CompareTag("Enemy"))
{
Destroy(this.gameObject);
Destroy(collision.gameObject);
Instantiate(effectPrefab, collision.transform.position, Quaternion.identity);
AudioSource.PlayClipAtPoint(sound, transform.position);
}
// ★追加(ソニックビーム無効)
else if(collision.CompareTag("Wizard"))
{
Destroy(this.gameObject);
Instantiate(noDamageEffectPrefab, transform.position, Quaternion.identity);
AudioSource.PlayClipAtPoint(noDamageSound, transform.position);
}
}
}


剣攻撃は有効だが、ソニックビームは無効にする