1
\$\begingroup\$

I'm trying to create a random level generator where I have a load of rooms 16x10 and I generate them when the player enters the room. I want to check the space where I want to place a room, in this case up for if there's already a room there. I've written this code to do that but it doesn't appear to be working. Currently it should ALWAYS return true (and not run the code) because the object it is on has a hitbox. I want it to hit triggers because then I don't need a physical block in the centre of every room (hard when the rooms are even both ways)

Can you see what's wrong here? The code does spawn a block in the correct place.

if (!Physics.CheckSphere(new Vector2(transform.position.x, transform.position.y), 1f, 6, QueryTriggerInteraction.Collide))
        {
            //Get list of open bottoms
            getList("Assets/Room Lists/bottom.txt");
            //Make a random number from the list
            int newRoomIndex = random.Next(0, prefabObjects.Count);
            //Get that object
            GameObject newRoom = prefabObjects[newRoomIndex];
            //Place it in the world in the correct position
            Instantiate(newRoom, new Vector3(transform.position.x - 8, transform.position.y + 15, transform.position.z), Quaternion.identity);
        }
\$\endgroup\$
1
\$\begingroup\$

It appears that Physics.CheckSphere doesn't work with 2D colliders for some reason. I ended up using Physics2D.OverlapCircles instead.

Hope this can help someone out there :)

\$\endgroup\$
  • 1
    \$\begingroup\$ That "some reason" is that the Physics class refers to the 3D physics system powered by PhysX (the mention of a 3D volume like a sphere is a hint). The 2D physics system in Unity, Physics2D is an entirely separate simulation universe, powered by Box2D. The two physics worlds don't know about each other's colliders. \$\endgroup\$ – DMGregory Jan 21 '17 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.