我最近开始在 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/