rigidbody2d.GetContacts()是Unity中的一个方法,用于获取与该刚体(Rigidbody2D)相交的其他碰撞体(Collider2D)的信息。
1.关于public int GetContacts(Collider2D[] colliders)
使用方法:
- 首先,在脚本中获取到需要查询碰撞信息的刚体组件。例如:Rigidbody2D rb = GetComponent<Rigidbody2D>();
- 使用rb.GetContacts()来获取与该刚体相交的碰撞信息。例如:ContactPoint2D[] contacts = rb.GetContacts();
- 碰撞信息将存储在ContactPoint2D数组中。可以使用循环遍历contacts数组,以获取每个碰撞体的详细信息。例如: for(int i=0; i < contacts.Length; i++) { ContactPoint2D contact = contacts[i]; // 使用contact的属性和方法来获取碰撞体的信息,如碰撞点的位置、法线方向等。 }
2.关于public int GetContacts(ContactFilter2D contactFilter, ContactPoint2D[] contacts)
该方法是一个公共方法,返回类型为整数。
它接受两个参数:一个是ContactFilter2D类型的contactFilter对象,另一个是ContactPoint2D类型的数组contacts。
ContactFilter2D是一个用于过滤在2D物理引擎中的碰撞事件的过滤器。这个过滤器指定了在碰撞事件中哪些对象应该被考虑。
ContactPoint2D是一个结构体,它包含了在碰撞事件中两个碰撞体接触点的信息,如碰撞点的坐标、碰撞法线等。
这个方法的作用是获取满足指定过滤条件的碰撞事件的联系点,并将这些联系点存储在传入的contacts数组中。方法的返回值是存储在contacts数组中的联系点的数量。
总结起来,该方法用于获取满足指定过滤条件的碰撞事件的联系点,并返回联系点的数量。
Rigidbody2D rigidbody2d = GetComponent<Rigidbody2D>();
public ContactFilter2D pushContactFilter;//ContactFilter2D是一个用于过滤在2D物理引擎中的碰撞事件的过滤器。在引擎中可以通过这个过滤器指定了在碰撞事件中哪些对象应该被考虑
public ContactPoint2D []pushContactPoint=new ContactPoint2D[5];//存储接触物体
public rigidbody currentPushable;//查找角色移动中遇见的tag标签为TagName的物体的刚体
//数组内存储点信息的个数
int count = rigidbody2d.GetContacts(pushContactFilter, pushContactPoint);
if (count == 0){ return;}
for(int i = 0; i < count; i++)
{
//通过指定tag可以查找数组内符合的物体信息
if(pushContactPoint[i].rigidbody!=null
&&pushContactPoint[i].rigidbody.tag=="TagName")
{
currentPushable = pushContactPoint[i].rigidbody;
break;
}
else
{
currentPushable = null;
}
}
3.该方法拥有七种重载的方法
public int GetContacts(ContactFilter2D contactFilter, List<ContactPoint2D> contacts);
//
// 摘要:
// Retrieves all Colliders in contact with any of the Collider(s) attached to this
// rigidbody, with the results filtered by the ContactFilter2D.
//
// 参数:
// contactFilter:
// The contact filter used to filter the results differently, such as by layer mask,
// Z depth, or normal angle.
//
// colliders:
// An array of Collider2D used to receive the results.
//
// 返回结果:
// Returns the number of colliders placed in the colliders array.
public int GetContacts(ContactFilter2D contactFilter, Collider2D[] colliders);
public int GetContacts(List<Collider2D> colliders);
//
// 摘要:
// Retrieves all Colliders in contact with any of the Collider(s) attached to this
// Rigidbody.
//
// 参数:
// colliders:
// An array of Collider2D used to receive the results.
//
// 返回结果:
// Returns the number of colliders placed in the colliders array.
public int GetContacts(Collider2D[] colliders);
//
// 摘要:
// Retrieves all contact points for all of the Collider(s) attached to this Rigidbody,
// with the results filtered by the ContactFilter2D.
//
// 参数:
// contactFilter:
// The contact filter used to filter the results differently, such as by layer mask,
// Z depth, or normal angle.
//
// contacts:
// An array of ContactPoint2D used to receive the results.
//
// 返回结果:
// Returns the number of contacts placed in the contacts array.
public int GetContacts(ContactFilter2D contactFilter, ContactPoint2D[] contacts);
public int GetContacts(List<ContactPoint2D> contacts);
//
// 摘要:
// Retrieves all contact points for all of the Collider(s) attached to this Rigidbody.
//
// 参数:
// contacts:
// An array of ContactPoint2D used to receive the results.
//
// 返回结果:
// Returns the number of contacts placed in the contacts array.
public int GetContacts(ContactPoint2D[] contacts);
public int GetContacts(ContactFilter2D contactFilter, List<Collider2D> colliders);
注意事项:
- GetContacts()方法只能在FixedUpdate()或物理事件处理方法中调用,因为它需要在物理模拟步骤之后才能获取到正确的碰撞信息。
- 该方法只返回与刚体相交的直接碰撞体的信息,不包括间接碰撞体