Home > Back-end >  Unity3D how to check Collision with Objects tagged "tag"
Unity3D how to check Collision with Objects tagged "tag"

Time:12-20

at the moment, I am programming a Game with a 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.

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. 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. Thank you for help and I hope I can help someone with this Question too in the future!

CodePudding user response:

There is a third parameter for the Physics.CheckSphere function, which is called layerMask. You can specify this to selectively ignore colliders.

  • Related