Collision.contacts 接触点列表

var contacts : ContactPoint[]

Description描述

The contact points generated by the physics engine.

接触点由物理引擎产生。

Every contact contains a contact point, normal and the two colliders that collided (see ContactPoint). From inside OnCollisionStay or OnCollisionEnter you can always be sure that contacts has at least one element.

每一个接触(contact)包含一个接触点、法线和两个碰撞的碰撞器(看ContactPoint)。在OnCollisionStayOnCollisionEnter里你确保contacts至少有一个元素。

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	void OnCollisionStay(Collision collision) {
		foreach (ContactPoint contact in collision.contacts) {
			print(contact.thisCollider.name + " hit " + contact.otherCollider.name);
			Debug.DrawRay(contact.point, contact.normal, Color.white);
		}
	}
}
function OnCollisionStay(collision : Collision) {
	// Check if the collider we hit has a rigidbody
	// Then apply the force
	//检查如果碰撞的碰撞器有刚体,然后应用力
	for (var contact : ContactPoint in collision.contacts) {
		print(contact.thisCollider.name + " hit " + contact.otherCollider.name);
		// Visualize the contact point
		//可视化接触点
		Debug.DrawRay(contact.point, contact.normal, Color.white);
	}
}
using UnityEngine;
using System.Collections;

public class example : 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);
		Destroy(gameObject);
	}
}
// 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
	//旋转物体,y轴面沿着表面的法线。
	var contact = collision.contacts[0];
	var rot = Quaternion.FromToRotation(Vector3.up, contact.normal);
	var pos = contact.point;
	Instantiate(explosionPrefab, pos, rot);
	// Destroy the projectile
	//销毁手榴弹
	Destroy (gameObject);
}
Page last updated: 2010-12-18