爆風機能の追加

爆風の追加
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Blast : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip sound;
// 爆発の威力
private float power = 10f;
// 爆風の範囲(半径)
private float radius = 10f;
private void OnCollisionEnter(Collision collision)
{
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
AudioSource.PlayClipAtPoint(sound, transform.position);
// アイテムの位置が爆心地(ここを中心に周囲に爆風を及ぼす)
Vector3 explosionPos = transform.position;
// 爆心地から指定した範囲内にあるオブジェクトのColliderを取得する。
Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);
foreach(Collider hit in colliders)
{
Rigidbody rb = hit.GetComponent<Rigidbody>();
if(rb != null)
{
rb.AddExplosionForce(power, transform.position, radius, 1.0f, ForceMode.VelocityChange);
}
}
Destroy(gameObject);
}
}






【2019版】X_Mission(基礎/全51回)
他のコースを見る
爆風の追加
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Blast : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip sound;
// 爆発の威力
private float power = 10f;
// 爆風の範囲(半径)
private float radius = 10f;
private void OnCollisionEnter(Collision collision)
{
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
AudioSource.PlayClipAtPoint(sound, transform.position);
// アイテムの位置が爆心地(ここを中心に周囲に爆風を及ぼす)
Vector3 explosionPos = transform.position;
// 爆心地から指定した範囲内にあるオブジェクトのColliderを取得する。
Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);
foreach(Collider hit in colliders)
{
Rigidbody rb = hit.GetComponent<Rigidbody>();
if(rb != null)
{
rb.AddExplosionForce(power, transform.position, radius, 1.0f, ForceMode.VelocityChange);
}
}
Destroy(gameObject);
}
}






爆風機能の追加