返回介绍

Collider.OnCollisionEnter(Collision) 当碰撞进入

发布于 2019-12-18 15:37:32 字数 3195 浏览 1234 评论 0 收藏 0

Description 描述

OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider.

当该collider/rigidbody开始碰到另一个rigidbody/collider时OnCollisionEnter被调用。

In contrast to OnTriggerEnter, OnCollisionEnter is passed the Collision class and not a Collider. The Collision class contains information about contact points, impact velocity etc. If you don't use collisionInfo in the function, leave out the collisionInfo parameter as this avoids unneccessary calculations. Notes: Collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached. Collision events will be sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions.

相比OnTriggerEnter,OnCollisionEnter传递Collision类而不是Collider。Collision类包含接触点,碰撞速度等信息。在这个函数如果你不使用collisionInfo,删去collisionInfo参数以避免不必要的计算。注意,当其中至少一个碰撞器附加非动力学刚体时碰撞事件才会发送。碰撞事件也发送给禁用的MonoBehaviours,允许启用Behaviour来相应碰撞。

JavaScript:

	function OnCollisionEnter(collision : Collision) {
		// Debug-draw all contact points and normals
		for (var contact : ContactPoint in collision.contacts) {
			Debug.DrawRay(contact.point, contact.normal, Color.white);
		}
 
		// Play a sound if the coliding objects had a big impact.		
		if (collision.relativeVelocity.magnitude > 2)
			audio.Play();
	}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    void OnCollisionEnter(Collision collision) {
        foreach (ContactPoint contact in collision.contacts) {
            Debug.DrawRay(contact.point, contact.normal, Color.white);
        }
        if (collision.relativeVelocity.magnitude > 2)
            audio.Play();
 
    }
}

另一个例子:

JavaScript:

	// A grenade
	// - instantiates a explosion prefab when hitting a surface
	// - then destroys itself
 
	var explosionPrefab : Transform;
 
	function OnCollisionEnter(collision : Collision) {
		// Rotate the object so that the y-axis faces along the normal of the surface
		var contact : ContactPoint = collision.contacts[0];
		var rot : Quaternion = Quaternion.FromToRotation(Vector3.up, contact.normal);
		var pos : Vector3 = contact.point;
		Instantiate(explosionPrefab, pos, rot);
		// Destroy the projectile
		Destroy (gameObject);
	}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    public Transform explosionPrefab;
    void OnCollisionEnter(Collision collision) {
        ContactPoint contact = collision.contacts[0];
        Quaternion rot = Quaternion.FromToRotation(Vector3.up, contact.normal);
        Vector3 pos = contact.point;
        Instantiate(explosionPrefab, pos, rot) as Transform;
        Destroy(gameObject);
    }
}

Collider

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

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

发布评论

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