Question by 
               mileswells93 · Oct 17, 2020 at 05:33 PM · 
                physics2dcollider2doverlap  
              
 
              OverlapBox not working as expected
Hi everyone. I'm trying to create game objects by a player clicking, but only if the created object wouldn't overlap with any others. Relevant block:
 // left click
 if (Input.GetMouseButtonDown(0))
 {
   // mouse position on viewport
   var pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
   pos.z = 1;
 
   // menu item is selected
   if (activeItem != null)
   {
     var rectSize = this.activeItem.sprite.rect.size;
     var colliderSize = Camera.main.ScreenToViewportPoint(rectSize);
 
     var collidedWith = Physics2D.OverlapBox(pos, colliderSize, 0);
     // if the new object won't overlap, go ahead and create it.
     if (collidedWith == null)
     {
       var newObj = new GameObject(activeItem.label); // create game object
       newObj.AddComponent(System.Type.GetType(activeItem.className)); // add script
       var collider = newObj.AddComponent<BoxCollider2D>(); // add collider
       var renderer = newObj.AddComponent<SpriteRenderer>(); // add sprite renderer
 
       // rendering and positioning stuff 
       renderer.sprite = activeItem.sprite;
       renderer.sortingLayerName = "GameObjects";
       renderer.transform.position = pos;
     }
   }
 }
 
                
               This mostly works, but still allows overlap on roughly half of an object:

My goal is for the player to be unable to place objects overlapping at all. Thanks in advance for any advice!
 
                 
                problem.png 
                (5.2 kB) 
               
 
              
               Comment
              
 
               
              Your answer
 
             Follow this Question
Related Questions
2D prefabs(Enemy) overlap issue 0 Answers
i need help making this code work with 2D physics. 0 Answers
A box collider 2D (Is Trigger marked) stops my player from moving which has rigidbody 2D 1 Answer
How to add space between 2D Rigidbody objects without using colliders 0 Answers
Top-down collisiondetection 0 Answers