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);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。