我正在创建一个2D游戏,我想制作一个游戏,以便每当弹丸与瓷砖碰撞时,瓷砖就可以被破坏。问题是我不知道如何获得与弹丸相撞的瓷砖,我想制造它以便使其受损。
但是现在我要制造它,以便一旦弹丸落到地面上就破坏它坐在上面的瓷砖,我做了一个collider2d来帮助我,但是我仍然不知道如何获得这样的瓷砖弹丸坐着。
IEnumerator OnCollisionEnter2D(Collision2D collision)
{
//Projectile has reached the ground and is in collision with some tile
Vector2 hit = gameObject.transform.position;
Debug.Log("y" + hit.y);
hit.y =- 3; // i have no idea what y i should put in order to get the tile
if (x.gameObject.tag != "Player")
{
Collider2D[] collidedwith = Physics2D.OverlapCircleAll(this.gameObject.transform.position, radius);
//what should the hit vector be in order to destroy the tile.
tilemap.SetTile(tilemap.WorldToCell(hit), null);
}
}
用于更好解释的屏幕截图(我想摆脱蓝色的方块):
如果可能的话,我想将其制成aoe弹丸,但目前没有必要
答案 0 :(得分:0)
假设您有对撞机,即:
is trigger
的火箭对撞机(允许触发OnTriggerEnter2D
)OnTriggerEnter2D
现在,要检测火箭与瓷砖之间的碰撞,请执行以下操作:
tag
名称tile
脚本
// It will be triggered the the rocket crashes against the tile
void OnTriggerEnter2D(Collider2D col)
{
if(collision.gameObject.tag == "tile")
{
// Destroy the tile the rockets collided with
Destroy(collision.collider.gameObject);
// Destroy the rocket itself
Destroy(gameObject);
}
}
答案 1 :(得分:0)
好吧,我想出了一个解决方案,但该解决方案并没有真正想要的功能,我希望射弹造成AOE损坏
Tilemap tilemap = GetComponent<Tilemap>();
Vector3 hitPosition = Vector3.zero;
foreach (ContactPoint2D hit in collision.contacts)
{
Debug.Log(hit.point);
hitPosition.x = hit.point.x - 0.1f;
hitPosition.y = hit.point.y - 0.1f ;
tilemap.SetTile(tilemap.WorldToCell(hitPosition), null);
}