+-

void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Wall")
{
Destroy(gameObject);
}
}
我使用这个脚本.一切都很好,但是如果我的子弹在Wall中产生,它不会与wall反应.它在墙上飞.
那么,如果它在墙内产生,我应该怎么用来立即销毁子弹?
项目符号生成方法:
Instantiate(bullet1, firepoint1.position, firepoint1.rotation);

最佳答案
由于在其他碰撞器中实例化GameObject时,碰撞器给您带来麻烦.我建议您先检查子弹是否将在Wall内实例化,在这种情况下,请不要直接实例化它.我相信这将使代码更有效.
因此,首先需要在“图层”中添加“墙”(Here,您可以看到如何创建新图层并将墙分配给该图层).将其作为参数传递给用于实例化项目符号的脚本(例如,坦克的脚本).
public LayerMask wallLayer;
将储罐的变换保存在变量中
// Variable with position of the Tank
Transform _transform;
void Awake () {
_transform = GetComponent<Transform> ();
}
接下来,您将生成Physics2D.Linecast. Source.假设您使用附在坦克GameObject上的脚本来发射子弹:
// Linecase goes from the tank position to the place where you would be instantiating the bullet
boolean insideWall = Physics2D.Linecast(_transform.position, firepoint1.position, wallLayer);
然后,仅当您不会在墙内实例化子弹时,才实例化子弹.
if(!insideWall)
Instantiate(bullet1, firepoint1.position, firepoint1.rotation);
点击查看更多相关文章
转载注明原文:子弹实例化墙内,不触发OnCollisionEnter2D - 乐贴网