Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Tjoeker · Apr 09, 2020 at 10:25 AM · rotationcollisiontriggerwhile-loop

Rotate object as long as it's colliding

I'm creating a worldmap filled with pins/icons. This map is quite busy and to avoid pins being piled on top of each other, I'd like to rotate them so that in the end no pin is colliding with another.

Like this: pins

I reset the rotation back to (0,0,0) every time the user zooms in or out. So the rotation of all items should start over again.

But I can't find a way on how to do it properly.

I tried keeping a list of all collisions using OnTriggerEnter2D/OnTriggerExit2D. And then when the user scrolls I go through a custom method SetRotation:

 private void SetRotation()
     {
         transform.rotation = Quaternion.Euler(0, 0, 0);
 
         int tries = 5;
 
         while (collisions.Count > 0 && tries > 0)
         {
             if (collisions.All(a => a.transform.position.x <= transform.position.x))
             {
                 transform.rotation = Quaternion.Euler(0, 0, transform.rotation.z - 5);
             }
             else if (collisions.All(a => a.transform.position.x >= transform.position.x))
             {
                 transform.rotation = Quaternion.Euler(0, 0, transform.rotation.z + 5);
             }
 
             tries--;
         }
     }

The problem with this is that not all collisions are detected for some reason. And it only performs the rotation once, even though it should keep on rotating further to make it collision free.

Then I tried OnTriggerStay2D:

 private void OnTriggerStay2D(Collider2D collision)
     {
         while(RotationTries > 0)
         {
             if(collision.transform.position.x <= transform.position.x)
             {
                 transform.rotation = Quaternion.Euler(0, 0, transform.rotation.z - 5);
             }
             else if(collision.transform.position.x >= transform.position.x)
             {
                 transform.rotation = Quaternion.Euler(0, 0, transform.rotation.z + 5);
             }
 
             RotationTries--;
         }
     }

In this case all collisions get detected. (as far as I've seen so far). But still it only rotates items by 5° even though it needs more.

I use a limit of 5 tries for now, so it doesn't get stuck in an endless loop. I'll edit the maximum amount of tries later to get a better result.

Any thoughts on how I can make this work properly?

pins.png (10.7 kB)
Comment
Add comment · Show 2
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
avatar image azerty0220pl · Apr 09, 2020 at 10:52 AM 0
Share

Why do you use a while() loop in the OnTriggerStay2D() function? Try to remove it, so the function will execute once per frame if it is necessary.

avatar image Tjoeker azerty0220pl · Apr 09, 2020 at 11:49 AM 0
Share

I removed the while() loop. thanks for pointing that out.

While debugging I found out the transform.rotation.z - 5 is rarely executed, and if it is, it's often reset to 0 on its next run. Though the debugger never hits the reset I implemented. (unless it has to)

I vaguely remember this Euler can't go below 0. I'm now solving it differently. Thanks!

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

227 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Camera bounce back? 0 Answers

Changing layers affect triggers? 1 Answer

Adds too many points to the score? 0 Answers

Rigidbody2D.velocity out of controll 3 Answers

How to fix the position of a character when grabbing a rope? 1 Answer


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