vertex0 | 胶囊形状一端的位置。此点表示胶囊末端逻辑圆的中心点。 |
vertex1 | 胶囊形状另一端的位置。此点表示胶囊另一端逻辑圆的中心点。 |
radius | 胶囊的半径,定义了围绕vertex0 和 vertex1 以及它们之间区域的半径。 |
int 返回形状添加到 PhysicsShapeGroup2D 中的形状索引。此索引在检索形状时用作主要参考。
将胶囊形状(PhysicsShapeType2D.Capsule)添加到形状组。
胶囊形状由两个顶点(vertex0
和 vertex1
)定义的一个边缘形状和一个radius
组成,该半径延伸到边缘的长度和顶点周围。
注意:胶囊形成一个封闭的形状,并且具有内部,因此其内部可被物理查询等检测到。
using UnityEngine; using UnityEngine.Assertions;
public class Example : MonoBehaviour { void Start() { CreateShape(); GetShapeFromCollider(); }
// Create a custom shape. void CreateShape() { // Create a shape group. var shapeGroup = new PhysicsShapeGroup2D();
// Add a Capsule. var shapeIndex = shapeGroup.AddCapsule ( vertex0: Vector2.down, vertex1: Vector2.up, radius: 0.5f );
// Fetch the actual shape created. var physicsShape = shapeGroup.GetShape(shapeIndex);
// Validate what we created. Assert.AreEqual(PhysicsShapeType2D.Capsule, physicsShape.shapeType); Assert.AreApproximatelyEqual(0.5f, physicsShape.radius); Assert.AreEqual(2, physicsShape.vertexCount); }
// Get a physics shape from a Collider. void GetShapeFromCollider() { // Fetch the collider. var collider = GetComponent<CapsuleCollider2D>();
// If the collider is not active and enabled then we'll get no shapes. // Let's just quit if not. if (!collider.isActiveAndEnabled) return;
// Configure the collider. collider.size = new Vector2(1f, 2f);
// Create a shape group. var shapeGroup = new PhysicsShapeGroup2D();
// Get the collider shapes. collider.GetShapes(shapeGroup);
// Fetch the actual shape. var physicsShape = shapeGroup.GetShape(0);
// Validate what we retrieved. Assert.AreEqual(PhysicsShapeType2D.Capsule, physicsShape.shapeType); Assert.AreApproximatelyEqual(0.5f, physicsShape.radius); Assert.AreEqual(2, physicsShape.vertexCount); } }