大家好 :) 这是情况:
我有一个坏蛋预制件,其中包含两个碰撞器组件:一个简单的CapsuleCollider2D用于物理,以及一个仅用于鼠标点碰撞的触发器PolygonCollider2D。
PolygonCollider2D通过附加的脚本(Baddy.cs)仅在需要时进行更新,使用以下方法:
if(this.gameObject.GetComponent<PolygonCollider2D>()!=null){
Destroy(this.gameObject.GetComponent<PolygonCollider2D>());
}
this.gameObject.AddComponent<PolygonCollider2D>();
this.gameObject.GetComponent<PolygonCollider2D>().isTrigger=true;
是的,这样做不太好,但可以完成工作。它可以完美地工作:在游戏中的场景视图中,我可以暂停并查看我们的坏人预制件上确实有一个多边形碰撞器。
在另一个附加的脚本(CollisionHandler.cs)中,我们检查鼠标是否按下我们的坏人:
if(MousePress()){
hit=Physics2D.Raycast(level.mousePoint, Vector2.zero, 0f);
if(hit&&hit.collider==this.gameObject.GetComponent<PolygonCollider2D>()){
print("baddy mousePress");
}
}
然而,hit.collider==this.gameObject.GetComponent<PolygonCollider2D>()
的结果是 false,即使在我们的 if(MousePress())
中,我可以立即打印出 hit.collider
和 this.gameObject.GetComponent<PolygonCollider2D>()
并且它们都有定义:
D_Prefab(Clone) (UnityEngine.PolygonCollider2D)
由于我们的Baddy脚本同时删除和创建PolygonCollider,所以它们不能是不同的PolygonCollider,因为一次只能有一个。
您有什么想法可能出了什么问题吗?