Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Zaddo · Feb 07, 2021 at 11:39 AM · physicsscript.spawn

OverlapBoxNonAlloc not working correctly when called multiple times in single frame

I have an editor script that locates spawn locations. I use OverlapBoxNonALlloc to determine if a location is free of obstacles. In about 5% of cases, the OverlapBoxNonAlloc does not detect that there is a collision with an existing object.. I have gone over this every which way, but I can't understand why this is failing.

The GetPlacement method will loop and test random locations until it finds one that does not overlap with an existing object, or the maximum attempts are exceeded. When successful, it places a temporary cube at that location.

In the youtube video, you can see that the majority of cubes are placed in non-overlapping locations. But a small number of them overlap other cubes.

Video showing issue: https://youtu.be/8KnzTn9Hb68

    private bool GetPlacement(PoolDefinitionScriptableObject item, Bounds bounds)
     {
         Vector3 boxPosition = Vector3.zero;
 
         var position = RandomPointInBounds(bounds);
 
         // Ensure the spawn point is clear of any obstructing objects.
         if (item._CheckForObstruction)
         {
             var attempt = 0;
             var success = false;
             while (attempt < item._PlacementAttempts)
             {
 
                 boxPosition = position + Vector3.up * (item._Size.y * 0.5f + 0.2f);
 
                 // Check for collision
                 var overlapCount = Physics.OverlapBoxNonAlloc(boxPosition, item._Size * 0.5f, _ObstructionColliders, Quaternion.identity, item._ObstructionLayers, QueryTriggerInteraction.Ignore);
                 success = overlapCount == 0;
                 if (success)
                 {
                     break;
                 }
 
                 ++attempt;
                 position = RandomPointInBounds(bounds);
             }
 
             // No valid position was found - return false.
             if (!success)
             {
                 return false;
             }
         }
 
         // If the ground snap height is positive then the position should be located on the ground.
         if (item._GroundSnapHeight > 0)
         {
             RaycastHit raycastHit;
             if (Physics.Raycast(position + Vector3.up * item._GroundSnapHeight, -Vector3.up, out raycastHit, item._GroundSnapHeight + 0.2f, item._ObstructionLayers, QueryTriggerInteraction.Ignore))
             {
                 position = raycastHit.point + Vector3.up * 0.01f;
             }
         }
 
         CreateSpawnCube(position + (Vector3.up * (item._Size.y * 0.5f)), item._Size);
 
         return true;
     }
 
     public static GameObject CreateSpawnCube(Vector3 location, Vector3 size)
     {
         var shape = GameObject.CreatePrimitive(PrimitiveType.Cube);
         shape.transform.localScale = size;
         shape.transform.position = location;
 
         var material = (Material)Resources.Load("Red");
         if (material == null)
         {
             Debug.LogError("No material resource found");
 
             return null;
         }
 
         shape.GetComponent<Renderer>().material = material;
 
         return shape;
     }


[1]: https://youtu.be/8KnzTn9Hb68

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Zaddo · Feb 08, 2021 at 09:50 AM

I found the solution here: https://forum.unity.com/threads/solved-strange-behavior-with-overlapbox.833299/

Credit to @Baste

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

208 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get the Half-Major Axis from GameObject ? 0 Answers

How to respawn an object 1 Answer

help with instantiating gameobject at random postition from a target gameobject? 1 Answer

Save GameObject to file without Playerpref 0 Answers

Swinging an object in vr 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges