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
3
Question by ANGRYSH4RK · May 26, 2014 at 01:55 AM · spawningcircleradius

Spawn prefabs in a circle?

I found this http://answers.unity3d.com/questions/170413/position-objects-around-other-object-forming-a-cir.html but the example given is in UnityScript and I've had trouble converting it to C#.

Is there a better way of doing the following:

1) Spawn enemies along a circular perimeter around a Vector3 point.

2) Vary the distance from the point.

If that is the best way, could someone help convert it to C#? Here it is again:

 function RandomCircle(center:Vector3, radius:float): Vector3 {
     // create random angle between 0 to 360 degrees
     var ang = Random.value * 360;
     var pos: Vector3;
     pos.x = center.x + radius * Mathf.Sin(ang * Mathf.Deg2Rad);
     pos.y = center.y + radius * Mathf.Cos(ang * Mathf.Deg2Rad);
     pos.z = center.z;
     return pos;
 }
 
 // How to use:
 
 var center = transform.position;
 for (i = 0; i < numObjects; i++){
       var pos = RandomCircle(center, 10);
      // make the object face the center
      var rot = Quaternion.FromToRotation(Vector3.forward, center-pos);
      Instantiate(prefab, pos, rot);
 }
 


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 robertbu · May 26, 2014 at 02:10 AM 0
Share

This code looks fine. It uses a polar to rect conversion. An alternate approach (a bit more typical of Unity code) would be to use a Vector rotated around a point. Post your attempt at a C# conversion, and I'm sure we can help you over any rough spots. I see nothing out of the ordinary in the conversion.

avatar image ANGRYSH4RK · May 26, 2014 at 02:18 AM 0
Share
     Vector3 RandomCircle ( Vector3 center ,   float radius  ){
         Vector3 ang = Random.value * 360;
         Vector3 pos;
         pos.x = center.x + radius * $$anonymous$$athf.Sin(ang * $$anonymous$$athf.Deg2Rad);
         pos.y = center.y + radius * $$anonymous$$athf.Cos(ang * $$anonymous$$athf.Deg2Rad);
         pos.z = center.z;
         return pos;
     }
   
     Vector3 center = transform.position;
     for (i = 0; i < numObjects; i++){
          Vector3 pos = RandomCircle(center, 10);
          Vector3 rot = Quaternion.FromToRotation(Vector3.forward, center-pos);
          Instantiate(prefab, pos, rot);
     }


I get "parsing error" and "Class, struct, or interface method must have a return type".

3 Replies

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

Answer by robertbu · May 26, 2014 at 02:36 AM

Here is the completed C# translation. You had the type wrong on line 2 and line 13. You were missing an 'int' on line 11.

 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour {
 
     public int numObjects = 10;
     public GameObject prefab;
 
     void Start() {
         Vector3 center = transform.position;
         for (int i = 0; i < numObjects; i++){
             Vector3 pos = RandomCircle(center, 5.0f);
             Quaternion rot = Quaternion.FromToRotation(Vector3.forward, center-pos);
             Instantiate(prefab, pos, rot);
         }
     }
 
     Vector3 RandomCircle ( Vector3 center ,   float radius  ){
         float ang = Random.value * 360;
         Vector3 pos;
         pos.x = center.x + radius * Mathf.Sin(ang * Mathf.Deg2Rad);
         pos.y = center.y + radius * Mathf.Cos(ang * Mathf.Deg2Rad);
         pos.z = center.z;
         return pos;
     }
 }
Comment
Add comment · Show 6 · 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 ANGRYSH4RK · May 26, 2014 at 03:06 AM 0
Share

thank you for the help! errors are gone

avatar image JasonC001 · Oct 01, 2014 at 06:34 PM 0
Share

Great start. Now I just need to randomize the radius and I'll have what I've been looking for for my project too. :)

avatar image Noxury · Jul 01, 2016 at 02:23 PM 0
Share

There is some error when an object is instantiated at Vector3(0,0,AnyNegativeZValue), it will flip around 180° on its Z-Axis, e.g. the car is upside down.

avatar image Bunny83 Noxury · Jul 01, 2016 at 04:29 PM 0
Share

You shouldn't use Quaternion.FromToRotation in such a case. Use Quaternion.LookRotation(pos-center)

avatar image Noxury Bunny83 · Jul 03, 2016 at 02:43 PM 0
Share

Great, thanks!

avatar image jakethegamedev · Dec 27, 2018 at 10:51 PM 0
Share

i don't know if this is possible but the prefabs spawn far away from the object that they are meant to spawn on and i would love to make them spawn closer if you know how plz tell me

avatar image
8

Answer by CaptainTR · Nov 11, 2015 at 02:51 PM

alt texttry this code

 int numObjects = 12;
 public GameObject prefab;

 void Start()
 {
     Vector3 center = transform.position;

     for (int i = 0; i < numObjects; i++)
     {
         int a = i * 30;


         Vector3 pos = RandomCircle(center, 1.0f ,a);
         Instantiate(prefab, pos, Quaternion.identity);
     }

 }

 Vector3 RandomCircle(Vector3 center, float radius,int a)
 {
     Debug.Log(a);
     float ang = a;
     Vector3 pos;
     pos.x = center.x + radius * Mathf.Sin(ang * Mathf.Deg2Rad);
     pos.y = center.y + radius * Mathf.Cos(ang * Mathf.Deg2Rad);
     pos.z = center.z;
     return pos;
 }



paylas.jpg (154.8 kB)
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 Ziron999 · Nov 14, 2016 at 05:48 AM 0
Share

usually your plane will be 0,0,0 on all transforms to make it look right top down you would want to swap y/z:

  pos.z = center.z + radius * $$anonymous$$athf.Cos(ang * $$anonymous$$athf.Deg2Rad);
  pos.y = center.y;

avatar image IndieForger · Apr 29, 2017 at 10:59 PM 1
Share

You can replace

  int a = i * 30;

with

  int a = 360 / numObjects * i

which wil make a bit more dynamic.

avatar image thomaslay7 · Jun 02, 2018 at 09:29 PM 0
Share

how to make it using image? i had made like above . prefab = image. but when i try, the prefab out of canvas? when i try using 2d sprites, it's work like above.

avatar image
0

Answer by tesan · Aug 09, 2017 at 07:33 PM

you can watch this plugin: http://u3d.as/Un7, I think it's what you want

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

29 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

Related Questions

radiusX and radiusY ? 1 Answer

Rotate around a circle 0 Answers

Instantiate gameobjects in a segment of a circle if you have radius of the cricle 2 Answers

Find distance around a circle, from center point and another point 2 Answers

Getting radius around multiple transform 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