【问题标题】:How to find which side of a collider has been hit如何找到对撞机的哪一侧被击中
【发布时间】:2023-01-08 21:58:48
【问题描述】:

嗨 Guyz 我无法找到一种方法如何找到对撞机的哪一侧被击中......我有一辆汽车及其包含的箱式对撞机......我想要的是另一侧车撞到我的车然后我会增加一些力……但首先我需要检测车的哪一侧撞到……

嗨 Guyz 我无法找到一种方法如何找到对撞机的哪一侧被击中......我有一辆汽车及其包含的箱式对撞机......我想要的是另一侧车撞到我的车然后我会增加一些力……但首先我需要检测车的哪一侧撞到……

【问题讨论】:

  • 检查它的生命值

标签: unity3d collider


【解决方案1】:

1 - 您可以遍历接触点并检查法线。

void OnCollisionEnter(Collision collision)
{
    // Loop through all contact points
    foreach (ContactPoint contact in collision.contacts)
    {
        // Check the normal of each contact point
        if (contact.normal.y > 0)
        {
            // The top of the collider was hit
            Debug.Log("Top hit");
        }
        else if (contact.normal.y < 0)
        {
            // The bottom of the collider was hit
            Debug.Log("Bottom hit");
        }
        else if (contact.normal.x > 0)
        {
            // The right side of the collider was hit
            Debug.Log("Right hit");
        }
        else if (contact.normal.x < 0)
        {
            // The left side of the collider was hit
            Debug.Log("Left hit");
        }
    }
}

您还可以使用第零个索引来检查最近的点,而不是遍历所有点。

2 - 您可以使用对撞机的ClosestPoint

3 - 检查方向

private void OnTriggerEnter (Collider other) {

     Vector3 direction = other.transform.position - transform.position;
     if (Vector3.Dot (transform.forward, direction) > 0) {
         print ("Back");
     } 
     if (Vector3.Dot (transform.forward, direction) < 0) {
         print ("Front");
     } 
     if (Vector3.Dot (transform.forward, direction) == 0) {
         print ("Side");
     }
}

【讨论】:

  • Thankx man 但我解决了
【解决方案2】:

我通过这段代码解决了

碰撞进入 Vector3 dir = collision.transform.position - transform.position; Getcomponent().AddForce(dir *50)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-11
    相关资源
    最近更新 更多