Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 captures
13 Jun 22 - 14 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
0
Question by christophervauhgan22 · May 07 at 01:48 PM · instantiatecollider2dboundsoverlap

Getting Collider2D.bounds.extents to update immediately (2D)

for a while now I have been trying to solve a problem with newly instantiated 2D game objects spawning in each other. these objects can have any scale, any position, and any rotation.

my original solution was to spawn one, set rotation, position, and scale, and use something like Collider2D.OverlapCollider to find colliders inside of it, but the problem with this is I am spawning all these objects in a single frame, so the rot, pos, and scale don't update in time for this method to work / other colliders aren't placed in the world in time (idk I didn't do more testing, it just showed up as 0 colliders, or 1 if it was over a pre-existing game object like the player)

after trying some other solutions I landed on one that would work: using Physics2D.OverlapAreaAll to find all colliders in an area. to get this area I need the min and max bounds of the collider (collider2D.bounds.min, collider2D.bounds.max).

the value for these is always pos +/- extents - but here's the problem: if you spawn the game object, change pos, rot and scale the bounds.extents don't update till the next frame, therefore the area Physics2D.OverlapAreaAll checks in is always the size of the prefab scale and in the prefab rotation.

I tried to calculate the new extents myself, which would be great if it worked as I don't need to spawn a bunch more objects to check if a place is clear.

I started simple and kept adding more but I'm not a maths genius so I can't figure out the exact maths I need to get it right 100% of the time. the following is what I am up to:

 Vector3 position = new Vector3(UnityEngine.Random.Range(minX, maxX), UnityEngine.Random.Range(minY, maxY), 0);
 Vector3 localScale = new Vector3(UnityEngine.Random.Range(-obj.Value, obj.Value), UnityEngine.Random.Range(-obj.Value, obj.Value), 0);
 Quaternion rotation = Quaternion.Euler(0, 0, UnityEngine.Random.Range(0.0f, 360.0f));
 
 double extentX = (localScale.x / 2) + (Math.Pow(Math.Sin(((localScale.x + localScale.y) / Math.Sqrt(Math.Pow(localScale.x, 2) + Math.Pow(localScale.y, 2))) * ((Math.PI / 180) * rotation.eulerAngles.z)), 2) * (Math.Sqrt(2 * Math.Pow((localScale.x / 2), 2)) - (localScale.x / 2)));
 double extentY = (localScale.y / 2) + (Math.Pow(Math.Sin(((localScale.x + localScale.y) / Math.Sqrt(Math.Pow(localScale.x, 2) + Math.Pow(localScale.y, 2))) * ((Math.PI / 180) * rotation.eulerAngles.z)), 2) * (Math.Sqrt(2 * Math.Pow((localScale.y / 2), 2)) - (localScale.y / 2)));
 Vector3 extents = new Vector3((float)extentX, (float)extentY, 0);
 Vector3 max = position + extents;
 Vector3 min = position - extents;
 
 //Debug.Log(max);
 //Debug.Log(min);
 
 Collider2D[] colliders = Physics2D.OverlapAreaAll(max, min);

the big problem with this is it only finds the extent up to the first maxima (when extent x/y is the greatest), I amended this to work for the first maxima and the next 180 degrees away (the image attached shows examples of graphs for this, as each one finds 2 maxima and there are 4, there are two graphs) but doing this made the extent never reach its minimas (when extent x/y is the lowest), and as such, I kept with the implementation above.

alt text



I tried to look for a method that would update the extents right then and there but I couldn't find anything. I even went looking in unity source code to find the formula they use to calculate bounds.extents but couldn't find anything relevant.

It is necessary that all the game object spawning is done in a single frame, so coroutines wouldn't work for me.

If anyone has the formula unity uses for extents, or a way to check overlapping colliders that would work for me, I'm all ears :)

desmos-unity-question-grpahs.png (127.2 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

162 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

Related Questions

Spawn GameObjects without overlap 1 Answer

Determine if two meshes overlap, without using bounds or trigger? 0 Answers

check box overlap / colliders in edit mode 0 Answers

How to not instantiate an object if there is another object there? 1 Answer

Physics2D.OverlapBoxAll and Physics2D.OverlapAreaAll are inaccurate. 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