c# - Unity 2D 碰撞检测

标签 c# unity-game-engine box2d collision-detection

我最近开始在 Unity 中开发我的第一个 2D 游戏,但我遇到了碰撞检测问题。有没有简单的方法来获取侧面的碰撞检测?我的对象有一个 Rigidbody2D 和一个 BoxCollider2D

最佳答案

团结OnCollisionEnter2D方法为您提供了对与您的游戏对象接触的碰撞器的引用。因此,您可以将您的游戏对象的位置与击中您的游戏对象的位置进行比较。例如:

void OnCollisionEnter2D(Collision2D coll) 
{
    Vector3 collPosition = coll.transform.position;

    if(collPosition.y > transform.position.y)
    {
        Debug.Log("The object that hit me is above me!");
    }
    else
    { 
        Debug.Log("The object that hit me is below me!");
    }

    if (collPosition.x > transform.position.x)
    {
        Debug.Log ("The object that hit me is to my right!");
    } 
    else 
    {
        Debug.Log("The object that hit me is to my left!");
    }
}

关于c# - Unity 2D 碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32872196/

相关文章:

c# - 如何使用 Facebook C# SDK 登录 Facebook

c# - 以多种形式调用方法时类变量被重置

c# - 交换 3 个六边形邻居的引用

box2d - 破坏关节时出错

c# - AutoMapper 可以在值类型(枚举)和引用类型之间进行映射吗? (字符串)

C# - 应用程序.Run()

unity-game-engine - 通过脚本隐藏/取消隐藏 Unity3D 中的对象

google-chrome - 适用于 Chrome 的 Unity WebGL [需要修复浏览器]

ios - 更改 Box2D anchor ?

2d - Box2D 中一米是多少像素?