返回介绍

Rigidbody.OnCollisionEnter(Collision) 当碰撞进入

发布于 2019-12-18 15:38:28 字数 3704 浏览 1005 评论 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类。Collision类包含有关接触点、碰撞速度等的信息。如果在函数中不使用collisionInfo,省略collisionInfo参数以避免不必要的计算。注意,当其中至少一个碰撞器附加非动力学刚体时碰撞事件才会发送。碰撞事件也发送给禁用的MonoBehaviours,允许启用Behaviour来相应碰撞。

JavaScript:

#pragma strict
var audio: AudioSource;
function Start() {
	audio = GetComponent.<AudioSource>();
}
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 colliding objects had a big impact.		
	if (collision.relativeVelocity.magnitude > 2)
		audio.Play();
}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
	AudioSource audio;
 
	void Start() {
		audio = GetComponent<AudioSource>();
	}
 
 
    void OnCollisionEnter(Collision collision) {
    	// Debug-draw all contact points and normals
        foreach (ContactPoint contact in collision.contacts) {
            Debug.DrawRay(contact.point, contact.normal, Color.white);
        }
 
        // Play a sound if the colliding objects had a big impact.		
        if (collision.relativeVelocity.magnitude > 2)
            audio.Play();
 
    }
}

另一个例子:

JavaScript:

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

C#:

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

Rigidbody

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

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

发布评论

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