简体   繁体   English

Unity3D 如何检查与标记为“标签”的对象的碰撞

[英]Unity3D how to check Collision with Objects tagged "tag"

at the moment, I am programming a Game with a Random GameObject Spawner .目前,我正在使用Random GameObject Spawner编写游戏。 My problem to solve is that I am using Physics.Checksphere to Instantiate Objects only if there is no Object at this Point around the radius I want.我要解决的问题是,只有在我想要的半径周围的这个点没有 Object 时,我才使用Physics.Checksphere来实例化对象。

That is my Code for this implementation:这是我的实现代码:

if(!Physics.CheckSphere(pos, spawnCollisionCheckRadius))                //Check Radius at pos
       {
           GameObject actStone = stones[Random.Range(0, stones.Count)]; //Get Random Stone Object from List
           Instantiate(actStone, pos, rot);                             //Spawn Stone
       }

So the issue what I actually have I that when I have a Sphere Collider my Code with Physics.CheckSphere is not working, because it checks any Collision in this Sphere around the Stone and every time it gets a Collision with my planet.所以我实际拥有的问题是,当我有一个球体碰撞器时,我的Physics.CheckSphere代码不起作用,因为它会检查石头周围这个球体中的任何碰撞,并且每次它与我的星球发生碰撞时。 But I need a Collider around my Planet.但我需要一个环绕我的星球的对撞机。 How can I only check a Collision with a tag for me "Stone" and not all Collisions in this Radius.我怎样才能只为我检查带有“石头”标签的碰撞,而不是这个半径中的所有碰撞。 My thoughts were maybe to use Physics.OverlapSphere somehow, but I actually did not know how to implement it.我的想法可能是以某种方式使用Physics.OverlapSphere ,但我实际上不知道如何实现它。 Thank you for help and I hope I can help someone with this Question too in the future!感谢您的帮助,我希望将来我也可以帮助解决这个问题!

There is a third parameter for the Physics.CheckSphere function, which is called layerMask . Physics.CheckSphere还有第三个参数 function,称为layerMask You can specify this to selectively ignore colliders.您可以指定它以选择性地忽略碰撞器。

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

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