返回介绍

MonoBehaviour.OnParticleCollision(GameObject) 当粒子碰撞

发布于 2019-12-18 15:38:03 字数 8585 浏览 1635 评论 0 收藏 0

Description 描述

OnParticleCollision is called when a particle hits a collider.

当粒子碰到碰撞器时,OnParticleCollision 被调用。

This can be used to apply damage to a game object when hit by particles.

这个可以用于游戏对象被粒子击中时应用伤害到它上面。

Legacy particle system: 老的粒子系统

This message is sent to all scripts attached to the WorldParticleCollider and to the Collider that was hit. The message is only sent if you enable sendCollisionMessage in the inspector of the WorldParticleCollider.

这个消息被发送到所有附加到theWorldParticleCollider 的脚本上和被击中的碰撞体上。这个消息只有当你在theWorldParticleCollider 检视面板中启用了sendCollisionMessage 才会被发送。

JavaScript:

// 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); 
	} 
} 

C#:

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);
        }
    }
}

Shuriken particle system: 忍者飞镖例子系统

This message is sent to scripts attached to particle systems and to the Collider that was hit.

这个消息发送给碰到碰撞器并附件粒子系统的脚本。

When OnParticleCollision is invoked from a script attached to a GameObject with a Collider the GameObject parameter represents the ParticleSystem. The Collider receives at most one message per particle system that collided with it in any given frame even when the particle system struck the Collider with multiple particles in the current frame. In order to retrieve detailed information about all the collisions caused by the ParticleSystem ParticleSystem.GetCollisionEvents must be used to retrieve the array of ParticleSystem.CollisionEvent.

当OnParticleCollision从附加到带有刚体的游戏对象的脚本调用时,游戏对象的参数表示粒子系统。该碰撞器在任何给定帧接收每个粒子系统碰撞最多一条消息,甚至当粒子系统在当前帧碰到带有多个粒子的碰撞器时。为了检索所有的碰撞的粒子系统的详细信息,ParticleSystem.GetCollisionEvents必须用来检索ParticleSystem.CollisionEvent数组。

When OnParticleCollision is invoked from a script attached to a ParticleSystem the GameObject parameter represents a GameObject with an attached Collider struck by the ParticleSystem. The ParticleSystem will receive at most one message per Collider that is struck. As above ParticleSystem.GetCollisionEvents must be used to retrieve all the collisions incident on the GameObject.

当OnParticleCollision从附加到粒子系统的脚本调用时,游戏对象的参数表示附件有碰撞器由粒子系统碰到的对象。该粒子系统将在每碰撞器最多接收一条消息。如上,ParticleSystem.GetCollisionEvents 必须用来检索所有的碰撞事件的对象。

Messages are only sent if you enable /Send Collision Messages/ in the inspector of the particle system collision module.

在检视面板的粒子碰撞栏Send Collision Messages启用,消息才会发送。

OnParticleCollision can be a co-routine, simply use the yield statement in the function.

OnParticleCollision 可以被用作协同程序,在函数中调用yield语句。

JavaScript:

// 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); 
		} 
	}
} 

JavaScript:

// 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); 
		}
	}
} 

C#:

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++;
        }
    }
}

C#:

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++;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文