言語: 日本語
  • C#
  • JS
  • Boo

スクリプト言語

お好みのスクリプト言語を選択すると、サンプルコードがその言語で表示されます。

MonoBehaviour.OnParticleCollision(GameObject)

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual

Description

OnParticleCollisionはパーティクルがコライダーにヒットした時に呼び出されます

これはパーティクルがヒットした時にゲームオブジェクトにダメージを適用させるために使用することができます。 レガシー パーティクルシステム:
このメッセージは WorldParticleCollider をアタッチしたオブジェクトのすべてのスクリプトと、ヒットしたColliderに送信されます。 このメッセージは WorldParticleCollider のインスペクターにある sendCollisionMessage を有効にした時のみ送信されます。

	// Legacy particle system example
	// Applies a force to all rigid bodies that are hit by the particle.

	function OnParticleCollision (other : GameObject) {
		var body : Rigidbody = other.rigidbody;
		if (body) {
			var direction : Vector3 = other.transform.position - transform.position;
			direction = direction.normalized;
			body.AddForce (direction * 5);
		}
	}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void OnParticleCollision(GameObject other) {
        Rigidbody body = other.rigidbody;
        if (body) {
            Vector3 direction = other.transform.position - transform.position;
            direction = direction.normalized;
            body.AddForce(direction * 5);
        }
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	def OnParticleCollision(other as GameObject) as void:
		body as Rigidbody = other.rigidbody
		if body:
			direction as Vector3 = (other.transform.position - transform.position)
			direction = direction.normalized
			body.AddForce((direction * 5))

Shuriken パーティクルシステム:
このメッセージはパーティクルシステムにアタッチされたスクリプトとヒットしたCollider に対して送信されます。 OnParticleCollisionがColliderを持つGameObjectにアタッチされたスクリプトから実行された時、GameObjectパラメータはParticleSystemのを表します。現在のフレームで複数のパーティクルとColliderがヒットした時、毎フレーム、コライダーと衝突したパーティクルシステムごとに最大1メッセージをColliderは受信します。ParticleSystemによって引き起こされたすべての衝突についての詳細な情報を取得するためには、ParticleSystem.GetCollisionEventsを使用してCollisionEventの配列を取得します。 OnParticleCollisionがParticleSystemにアタッチされたスクリプトから実行されている時、GameObjectパラメータはParticleSystemによってヒットしたColliderGameObjectを表します。ParticleSystemはヒットしたColliderごとに最大1つのメッセージを受信します。上記のようにParticleSystem.GetCollisionEventsを使用するとGameObject上のすべての衝突を受信することができます。 パーティクルシステムのインスペクターにあるCollisionモジュール内の /Send Collision Messages/ を有効にした時のみ、メッセージは送信されます。

OnParticleCollisionは関数の中にシンプルなyield文を使用して、コルーチンにすることができます。

	// collision event script attached to a GameObject
	// applies a force to rigid bodies that are hit by particles
	private var collisionEvents = new ParticleSystem.CollisionEvent[16];

	function OnParticleCollision(other : GameObject) {
		// get the particle system
		var particleSystem : ParticleSystem;
		particleSystem = other.GetComponent(ParticleSystem);

		// adjust array length
		var safeLength = particleSystem.safeCollisionEventSize;
		if (collisionEvents.Length < safeLength) {
			collisionEvents = new ParticleSystem.CollisionEvent[safeLength];
		}

		// get collision events for the gameObject that the script is attached to
		var numCollisionEvents = particleSystem.GetCollisionEvents(gameObject, collisionEvents);

		// apply some force to RigidBody components
		for (var i = 0; i < numCollisionEvents; i++) {
			if (gameObject.rigidbody)
			{
				var pos = collisionEvents[i].intersection;
				var force = collisionEvents[i].velocity * 10;
				gameObject.rigidbody.AddForce(force);
			}
		}
	}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    private ParticleSystem.CollisionEvent[] collisionEvents = new ParticleSystem.CollisionEvent[16];
    void OnParticleCollision(GameObject other) {
        ParticleSystem particleSystem;
        particleSystem = other.GetComponent<ParticleSystem>();
        int safeLength = particleSystem.safeCollisionEventSize;
        if (collisionEvents.Length < safeLength)
            collisionEvents = new ParticleSystem.CollisionEvent[safeLength];
        
        int numCollisionEvents = particleSystem.GetCollisionEvents(gameObject, collisionEvents);
        int i = 0;
        while (i < numCollisionEvents) {
            if (gameObject.rigidbody) {
                Vector3 pos = collisionEvents[i].intersection;
                Vector3 force = collisionEvents[i].velocity * 10;
                gameObject.rigidbody.AddForce(force);
            }
            i++;
        }
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	private collisionEvents as (ParticleSystem.CollisionEvent) = array[of ParticleSystem.CollisionEvent](16)

	def OnParticleCollision(other as GameObject) as void:
		particleSystem as ParticleSystem
		particleSystem = other.GetComponent[of ParticleSystem]()
		safeLength as int = particleSystem.safeCollisionEventSize
		if collisionEvents.Length < safeLength:
			collisionEvents = array[of ParticleSystem.CollisionEvent](safeLength)
		numCollisionEvents as int = particleSystem.GetCollisionEvents(gameObject, collisionEvents)
		i as int = 0
		while i < numCollisionEvents:
			if gameObject.rigidbody:
				pos as Vector3 = collisionEvents[i].intersection
				force as Vector3 = (collisionEvents[i].velocity * 10)
				gameObject.rigidbody.AddForce(force)
			i++

	// collision event script attached to a ParticleSystem
	// applies a force to rigid bodies that are hit by particles
	private var collisionEvents = new ParticleSystem.CollisionEvent[16];

	function OnParticleCollision(other : GameObject) {
		// adjust array length
		var safeLength = particleSystem.safeCollisionEventSize;
		if (collisionEvents.Length < safeLength) {
			collisionEvents = new ParticleSystem.CollisionEvent[safeLength];
		}

		// get collision events for the gameObject that the script is attached to
		var numCollisionEvents = particleSystem.GetCollisionEvents(other, collisionEvents);

		// apply some force to RigidBody components
		for (var i = 0; i < numCollisionEvents; i++) {
			if (other.rigidbody)
			{
				var pos = collisionEvents[i].intersection;
				var force = collisionEvents[i].velocity * 10;
				other.rigidbody.AddForce(force);
			}
		}
	}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    private ParticleSystem.CollisionEvent[] collisionEvents = new ParticleSystem.CollisionEvent[16];
    void OnParticleCollision(GameObject other) {
        int safeLength = particleSystem.safeCollisionEventSize;
        if (collisionEvents.Length < safeLength)
            collisionEvents = new ParticleSystem.CollisionEvent[safeLength];
        
        int numCollisionEvents = particleSystem.GetCollisionEvents(other, collisionEvents);
        int i = 0;
        while (i < numCollisionEvents) {
            if (other.rigidbody) {
                Vector3 pos = collisionEvents[i].intersection;
                Vector3 force = collisionEvents[i].velocity * 10;
                other.rigidbody.AddForce(force);
            }
            i++;
        }
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	private collisionEvents as (ParticleSystem.CollisionEvent) = array[of ParticleSystem.CollisionEvent](16)

	def OnParticleCollision(other as GameObject) as void:
		safeLength as int = particleSystem.safeCollisionEventSize
		if collisionEvents.Length < safeLength:
			collisionEvents = array[of ParticleSystem.CollisionEvent](safeLength)
		numCollisionEvents as int = particleSystem.GetCollisionEvents(other, collisionEvents)
		i as int = 0
		while i < numCollisionEvents:
			if other.rigidbody:
				pos as Vector3 = collisionEvents[i].intersection
				force as Vector3 = (collisionEvents[i].velocity * 10)
				other.rigidbody.AddForce(force)
			i++