C# 使用physics2d检测两个碰撞器的碰撞

C# 使用physics2d检测两个碰撞器的碰撞,c#,unity3d,C#,Unity3d,我有一个带有圆形碰撞器2d的对象,还有一个带有长方体碰撞器2d的对象。如何检测circlecollider何时撞击长方体碰撞器的上边缘。假设没有对象具有任何旋转。我想在代码C中这样做#当有东西与包含此脚本的对象发生冲突时触发,使用如下: void OnCollisionEnter(Collider collider) { foreach(CollisionPoint contact in collider.contacts) { //Do something

我有一个带有圆形碰撞器2d的对象,还有一个带有长方体碰撞器2d的对象。如何检测circlecollider何时撞击长方体碰撞器的上边缘。假设没有对象具有任何旋转。我想在代码C中这样做#

当有东西与包含此脚本的对象发生冲突时触发,使用如下:

void OnCollisionEnter(Collider collider)
{
    foreach(CollisionPoint contact in collider.contacts)
    {
        //Do something
    }
}
上述内容将为您提供碰撞的接触点列表,您可以通过该列表确定碰撞发生的位置。另一种方法是,如果只需要在指定位置检测碰撞,则可以在立方体中放置一个不可见的子对象,并在需要的位置放置一个碰撞器

编辑:

既然你提到了光线投射,我可以想出两种方法来实现它们。第一种方法是从立方体向上发射,但这存在仅从1点发射光线的问题,这意味着可能会错过一些碰撞(取决于立方体和球体的大小)

第二种方法是发射平行于立方体的光线。这听起来可能有点离经叛道,我还没有测试过它,但在理论上它应该可以工作。将其粘贴到多维数据集中:

void Update
{
    Vector3 start = this.transform.position;
    Vector3 end= this.transform.position;

    //This attempts to place the start & end point just above the cube
    //This of course assumes the cube isn't rolling around. If that's the case
    //then these calculations get quite a bit more complicated
    //Additionally the 0.01 might need adjusting if it's too high up off the cube
    start.y += this.renderer.bounds.y/2 + 0.01f;
    end.y += this.renderer.bounds.y/2 + 0.01f;

    start.x -= this.renderer.bounds.x/2;
    end.x += this.renderer.bounds.x/2;

    Ray ray = new Ray(start, end);
    RaycastHit hit;

    if(Physics.Raycast(ray, out hit, start.x-end.x) && hit.name == "mySphere")
    {
        //Theoretically, the sphere hit the top of the box!
    }
}
当某个对象与包含此脚本的对象发生冲突时触发,如下所示:

void OnCollisionEnter(Collider collider)
{
    foreach(CollisionPoint contact in collider.contacts)
    {
        //Do something
    }
}
上述内容将为您提供碰撞的接触点列表,您可以通过该列表确定碰撞发生的位置。另一种方法是,如果只需要在指定位置检测碰撞,则可以在立方体中放置一个不可见的子对象,并在需要的位置放置一个碰撞器

编辑:

既然你提到了光线投射,我可以想出两种方法来实现它们。第一种方法是从立方体向上发射,但这存在仅从1点发射光线的问题,这意味着可能会错过一些碰撞(取决于立方体和球体的大小)

第二种方法是发射平行于立方体的光线。这听起来可能有点离经叛道,我还没有测试过它,但在理论上它应该可以工作。将其粘贴到多维数据集中:

void Update
{
    Vector3 start = this.transform.position;
    Vector3 end= this.transform.position;

    //This attempts to place the start & end point just above the cube
    //This of course assumes the cube isn't rolling around. If that's the case
    //then these calculations get quite a bit more complicated
    //Additionally the 0.01 might need adjusting if it's too high up off the cube
    start.y += this.renderer.bounds.y/2 + 0.01f;
    end.y += this.renderer.bounds.y/2 + 0.01f;

    start.x -= this.renderer.bounds.x/2;
    end.x += this.renderer.bounds.x/2;

    Ray ray = new Ray(start, end);
    RaycastHit hit;

    if(Physics.Raycast(ray, out hit, start.x-end.x) && hit.name == "mySphere")
    {
        //Theoretically, the sphere hit the top of the box!
    }
}
当某个对象与包含此脚本的对象发生冲突时触发,如下所示:

void OnCollisionEnter(Collider collider)
{
    foreach(CollisionPoint contact in collider.contacts)
    {
        //Do something
    }
}
上述内容将为您提供碰撞的接触点列表,您可以通过该列表确定碰撞发生的位置。另一种方法是,如果只需要在指定位置检测碰撞,则可以在立方体中放置一个不可见的子对象,并在需要的位置放置一个碰撞器

编辑:

既然你提到了光线投射,我可以想出两种方法来实现它们。第一种方法是从立方体向上发射,但这存在仅从1点发射光线的问题,这意味着可能会错过一些碰撞(取决于立方体和球体的大小)

第二种方法是发射平行于立方体的光线。这听起来可能有点离经叛道,我还没有测试过它,但在理论上它应该可以工作。将其粘贴到多维数据集中:

void Update
{
    Vector3 start = this.transform.position;
    Vector3 end= this.transform.position;

    //This attempts to place the start & end point just above the cube
    //This of course assumes the cube isn't rolling around. If that's the case
    //then these calculations get quite a bit more complicated
    //Additionally the 0.01 might need adjusting if it's too high up off the cube
    start.y += this.renderer.bounds.y/2 + 0.01f;
    end.y += this.renderer.bounds.y/2 + 0.01f;

    start.x -= this.renderer.bounds.x/2;
    end.x += this.renderer.bounds.x/2;

    Ray ray = new Ray(start, end);
    RaycastHit hit;

    if(Physics.Raycast(ray, out hit, start.x-end.x) && hit.name == "mySphere")
    {
        //Theoretically, the sphere hit the top of the box!
    }
}
当某个对象与包含此脚本的对象发生冲突时触发,如下所示:

void OnCollisionEnter(Collider collider)
{
    foreach(CollisionPoint contact in collider.contacts)
    {
        //Do something
    }
}
上述内容将为您提供碰撞的接触点列表,您可以通过该列表确定碰撞发生的位置。另一种方法是,如果只需要在指定位置检测碰撞,则可以在立方体中放置一个不可见的子对象,并在需要的位置放置一个碰撞器

编辑:

既然你提到了光线投射,我可以想出两种方法来实现它们。第一种方法是从立方体向上发射,但这存在仅从1点发射光线的问题,这意味着可能会错过一些碰撞(取决于立方体和球体的大小)

第二种方法是发射平行于立方体的光线。这听起来可能有点离经叛道,我还没有测试过它,但在理论上它应该可以工作。将其粘贴到多维数据集中:

void Update
{
    Vector3 start = this.transform.position;
    Vector3 end= this.transform.position;

    //This attempts to place the start & end point just above the cube
    //This of course assumes the cube isn't rolling around. If that's the case
    //then these calculations get quite a bit more complicated
    //Additionally the 0.01 might need adjusting if it's too high up off the cube
    start.y += this.renderer.bounds.y/2 + 0.01f;
    end.y += this.renderer.bounds.y/2 + 0.01f;

    start.x -= this.renderer.bounds.x/2;
    end.x += this.renderer.bounds.x/2;

    Ray ray = new Ray(start, end);
    RaycastHit hit;

    if(Physics.Raycast(ray, out hit, start.x-end.x) && hit.name == "mySphere")
    {
        //Theoretically, the sphere hit the top of the box!
    }
}

第一种方法会产生问题,因为我觉得它不准确,我曾想过第二种方法,但这是我想要避免的方法,可以用Physics2d来完成吗。在你回复后,我会接受你的回答。我已经在答案中添加了一些代码,并提出了一个光线投射的想法。它可能实际上不起作用,因为我目前无法实际测试它,但试一下,让我知道它是如何运行的。我使用的是2d物理,我接近我想要实现的目标,唯一的问题是我没有使用layermask变量,假设ray会命中所有东西。谢谢你帮我找到正确的方向。你不能使用
void-OnCollisionEnter(碰撞器)
,你必须使用
void-OnCollisionEnter(碰撞器)
。第一种方法会带来问题,因为我觉得不准确,我曾想过第二种方法,但这是我想要避免的方法,可以用Physics2d.raycast来完成吗?我试过了,但没能成功。在你回复后,我会接受你的回答。我已经在答案中添加了一些代码,并提出了一个光线投射的想法。它可能实际上不起作用,因为我目前无法实际测试它,但试一下,让我知道它是如何运行的。我使用的是2d物理,我接近我想要实现的目标,唯一的问题是我没有使用layermask变量,假设ray会命中所有东西。谢谢你帮我找到正确的方向。你不能使用
void-OnCollisionEnter(碰撞器)
,你必须使用
void-OnCollisionEnter(碰撞器)
。第一种方法会带来问题,因为我觉得不准确,我曾想过第二种方法,但这是我想要避免的方法,可以用Physics2d.raycast来完成吗?我试过了,但没能成功。在你回复后,我会接受你的回答。我已经在答案中添加了一些代码,并提出了一个光线投射的想法。它可能实际上不起作用,因为我目前无法实际测试它,但给出了