Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
1
Question by pigaroos · Aug 29, 2019 at 08:25 PM · movecirclething

How to instantiate objects in a circle formation around a point?

Hello everyone, I would like to arrange some prefabs (eight to be more specific) in a circular formation around a center point, all equally spaced. They could be instantiated or just moved, I'm just trying to have them in circular formation. I've tried several supposed solutions from around the internet but each to no avail. What's the right thing to do?

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

5 Replies

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

Answer by Cornelis-de-Jager · Aug 29, 2019 at 10:04 PM

Explanation

If you don't really want to know just skip to final code snippet.


The circumferance of a circle is 2 * Pi * r but we can ignore the radius in this case since we only want the spread around the circle, making the function 2 * Pi. Lets say we have 5 enemies. This means each enemy must 2 * Pi / 5 distance around the circle (Radians) away from each other. We'll use num to denote number of enemies making the formula: 2 * Pi / num. If we were to loop through each enemy i, the position of the current one around the circle would be the previous position plus 2 * Pi / num. Meaning to determine the exact radians we can use 2 * Pi / num * i.

 for (int i = 0; i < num; i++){
         
     /* Distance around the circle */  
     var radians = 2 * MathF.Pi / num * i;
     ...
 }

We only have the radians now, but in unity we don't use radians so we need to convert radians into a direction (Vector3) and position (Vector3). To do that we need to fince both the vertcial and horizontal position. For vertical we can use Sin and horizontal we can use Cos. We can the add the new vector3 to the center point to get the spawn direction relative to center point and then multiply it by radius.

 /* Get the vector direction */ 
 var vertrical = MathF.Sin(radians);
 var horizontal = MathF.Cos(radians); 
         
 var spawnDir = new Vector3 (horizontal, 0, vertcial);
         
 /* Get the spawn position */ /* Get the spawn position */ 
 var spawnPos = point + spawnDir * radius; // Radius is just the distance away from the point
 

Now that we have the spawn pos we can simply spawn the enemy, rotate it to look at the player and then adjust height so the enemies don't fall out of the world.

 /* Now spawn */
 var enemy = Instantiate (enemyPefab, spawnPos, Qaurtenion.Identity) as GameObject;
         
 /* Rotate the enemy to face towards player */
 enemy.transform.LookAt(point);
         
 /* Adjust height */
 enemy.transform.Translate (new Vector3 (0, enemy.transform.LocalScale.y / 2, 0));
 

Full Solution

This is how it will look like if we put it all together:

 public GameObject enemyPefab;
 
 public void CreateEnemiesAroundPoint (int num, Vector3 point, float radius){
     
     for (int i = 0; i < num; i++){
         
         /* Distance around the circle */  
         var radians = 2 * MathF.Pi / num * i;
         
         /* Get the vector direction */ 
         var vertrical = MathF.Sin(radians);
         var horizontal = MathF.Cos(radians); 
         
         var spawnDir = new Vector3 (horizontal, 0, vertcial);
         
         /* Get the spawn position */ 
         var spawnPos = point + spawnDir * radius; // Radius is just the distance away from the point
         
         /* Now spawn */
         var enemy = Instantiate (enemyPefab, spawnPos, Qaurtenion.Identity) as GameObject;
         
         /* Rotate the enemy to face towards player */
         enemy.transform.LookAt(point);
         
         /* Adjust height */
         enemy.transform.Translate (new Vector3 (0, enemy.transform.LocalScale.y / 2, 0));
     }
 }    
 

Hope this helpds. If you have any questions let me know.

Comment
Add comment · Show 3 · 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
avatar image pigaroos · Aug 29, 2019 at 10:56 PM 0
Share

Thank you so much for sharing this! I was doing this for UI, so using transform.SetParent and moving the vertical from the Z axis to the Y axis did the trick. Thanks again!

avatar image HaromFarkas · Dec 23, 2020 at 04:17 PM 1
Share

you are some kind of sorceror.

avatar image briosh · Jun 10, 2021 at 07:44 PM 0
Share

Thank you! now i'll try to find how to do this on a sphere :)

avatar image
3

Answer by ssmas · Nov 06, 2021 at 06:54 AM

An easier way With normalizing the result you will get a point on circle diameter

 Random.insideUnitCircle.normalized * radius;





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

Answer by metixgosu · Mar 20, 2021 at 08:22 PM

@Cornelis-de-Jager why dont you check your code before post answer , so many mystakes in it.

vertrical - vertcial .Mathf not MathF etc...

Comment
Add comment · Show 1 · 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
avatar image
0

Answer by Riiich · Jan 24 at 12:40 PM

Here's @Cornelis-de-Jager's full solution answer but without the typos..

 public void CreateEnemiesAroundPoint(int num, Vector3 point, float radius)
 {
 
     for (int i = 0; i < num; i++)
     {
 
         /* Distance around the circle */
         var radians = 2 * Mathf.PI / num * i;
 
         /* Get the vector direction */
         var vertical = Mathf.Sin(radians);
         var horizontal = Mathf.Cos(radians);
 
         var spawnDir = new Vector3(horizontal, 0, vertical);
 
         /* Get the spawn position */
         var spawnPos = point + spawnDir * radius; // Radius is just the distance away from the point
 
         /* Now spawn */
         var enemy = Instantiate(enemyPefab, spawnPos, Quaternion.identity) as GameObject;
 
         /* Rotate the enemy to face towards player */
         enemy.transform.LookAt(point);
 
         /* Adjust height */
         enemy.transform.Translate(new Vector3(0, enemy.transform.localScale.y / 2, 0));
     }
 }
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
avatar image
0

Answer by Rhoekin · Feb 14 at 12:46 PM

For anyone struggling with overlapping objects change

  var radians = 2 * MathF.Pi / num * i;

To

 var radians = i* 2 * MathF.Pi / num ;

This solves the division by 0 error, plus spaces the objects like theyre supposed to be placed

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

117 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

Related Questions

How can move the ball around the circle? 1 Answer

A way to create an invisible circle for the enemy? 4 Answers

Move around a circle with iTween? 1 Answer

Sigh... Need help with moving object a distance over time 1 Answer

Moving object to specific location 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