简体   繁体   English

预制Collider2D未被识别为Raycast Collider

[英]Prefab Collider2D not Recognized as Raycast Collider

Hello everyone :) Here's the situation: 大家好:)这是情况:

I have a baddy prefab which has two collider components: a simple CapsuleCollider2D used for physics, and a trigger PolygonCollider2D used only for mouse point collisions. 我有一个baddy预制件,它有两个碰撞器组件:一个用于物理的简单CapsuleCollider2D,以及一个仅用于鼠标点碰撞的触发器PolygonCollider2D。

The PolygonCollider2D is updated through an attached script (Baddy.cs) only when needed using: PolygonCollider2D仅在需要时通过附加脚本(Baddy.cs)更新:

if(this.gameObject.GetComponent<PolygonCollider2D>()!=null){
     Destroy(this.gameObject.GetComponent<PolygonCollider2D>());
 }
 this.gameObject.AddComponent<PolygonCollider2D>();
 this.gameObject.GetComponent<PolygonCollider2D>().isTrigger=true;

Yes, this is poor practice, but it get's the job done. 是的,这是不好的做法,但它完成了工作。 It works perfectly: in the scene view of the game I can pause and see that there is indeed a polygon collider on our baddy prefab. 它完美地工作:在游戏的场景视图中我可以暂停并看到我们的baddy预制件上确实存在多边形对撞机。

In another attached script (CollisionHandler.cs) we check for a mouse press on our baddy: 在另一个附加脚本(CollisionHandler.cs)中,我们检查我们的baddy上的鼠标按下:

 if(MousePress()){
     hit=Physics2D.Raycast(level.mousePoint, Vector2.zero, 0f);
     if(hit&&hit.collider==this.gameObject.GetComponent<PolygonCollider2D>()){
         print("baddy mousePress");
     }
 }

However hit.collider==this.gameObject.GetComponent<PolygonCollider2D>() turns out false, even though I could print both hit.collider and this.gameObject.GetComponent<PolygonCollider2D>() immediately before hand in our if(MousePress()) and have them both defined as: 但是hit.collider==this.gameObject.GetComponent<PolygonCollider2D>()结果为false,即使我可以在我们的if(MousePress())之前立即打印hit.colliderthis.gameObject.GetComponent<PolygonCollider2D>() if(MousePress()) ,并让他们都定义为:

 D_Prefab(Clone) (UnityEngine.PolygonCollider2D)

They couldn't be different PolygonColliders because there can only be one at a time, since our Baddy script removes and creates the PolygonCollider simultaneously. 它们不能是不同的PolygonColliders,因为一次只能有一个,因为我们的Baddy脚本同时删除并创建了PolygonCollider。

Any ideas what could be going wrong? 什么想法可能会出错?

By making use of Physics2D.RaycastAll with a layerMask, I resolved the issue of other colliders getting in the way, including the baddy's own capsuleCollider2D. 通过使用带有layerMask的Physics2D.RaycastAll,我解决了其他碰撞器阻碍的问题,包括baddy自己的capsuleCollider2D。 Fortunately, my baddys would freeze before I needed to check for mouse collisions, so I updated the collider ahead of time when the baddy's anim speed was 0. Evidently creating and removing the colliders through an update function doesnt give the system enough time to handle the collisions. 幸运的是,在我需要检查鼠标碰撞之前,我的baddys会冻结,因此我提前更新了对手,当baddy的动画速度为0.显然通过更新功能创建和移除碰撞器并不能给系统足够的时间来处理碰撞。

http://answers.unity3d.com/questions/1326583/prefab-collider2d-not-recognized-as-raycast-collid.html#comment-1327337 http://answers.unity3d.com/questions/1326583/prefab-collider2d-not-recognized-as-raycast-collid.html#comment-1327337

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM