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
0
Question by Maniak101 · Aug 20, 2014 at 08:10 PM · c#beginner

Instantiating Prefabs around a point other than zero to create a circle

Hey guys,

This is my first post here and I'm new to Unity + programming in general. I'm learning c# and so have been heavily relying on the manual.

I need to place a number of prefabs around a point and have so far used this from the manual:

 void Start() {
     for (int i = 0; i < numberOfObjects; i++) {
         float angle = i * Mathf.PI * 2 / numberOfObjects;
         prefab.transform.position = transform.position; 
         Vector3 pos = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius; // original line
         Instantiate(prefab , pos, Quaternion.identity);
     }
 }

Now this works great and places the objects in a neat circle as expected, but, I'm trying to place them around the transform.position of the class that's instantiating them; the idea is that once placed I'll use RotateAround to rotate them around transform.position to form a shield that you can shoot out.

Any ideas? I'm totally lost and my math skills are non-existent.

Thanks in advance.

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

3 Replies

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

Answer by robertbu · Aug 20, 2014 at 08:11 PM

Just add the transform.position to your Instantiate():

   Instantiate(prefab, transform.position + pos, Quaternion.identity);
Comment
Add comment · Show 2 · 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 Maniak101 · Aug 20, 2014 at 10:14 PM 0
Share

Thanks for the replies. I tried to directly add the transform but it placed them very far way. This however fixed it.

 void Start() {
     for (int i = 0; i < numberOfObjects; i++) {
         float angle = i * $$anonymous$$athf.PI * 2 / numberOfObjects;
         Vector3 pos = new Vector3($$anonymous$$athf.Cos(angle), 0, $$anonymous$$athf.Sin(angle)) * radius; // original line

         currentPos = transform.position; 
         desiredPos = new Vector3 (currentPos.x + pos.x, currentPos.y + pos.y, currentPos.z + pos.z);
         prefab.transform.position = desiredPos;

         Instantiate(prefab , desiredPos, Quaternion.identity);
     }
 }

Thanks again guys!

Now to figure out how to add them as children :-s

avatar image rutter · Aug 20, 2014 at 10:16 PM 0
Share

Instantiate returns a reference to the object you just spawned. Grab its transform and set a parent for it.

I don't know what type prefab is, but every GameObject and component knows its associated transform:

 //assume it's a GameObject for now?
 //if it's some component, just fix the types in this example
 GameObject clone = (GameObject)Instantiate(prefab, desiredPos, Quaternion.identity);
 clone.transform.parent = someOtherTransform;
avatar image
0

Answer by stulleman · Aug 20, 2014 at 08:14 PM

Sorry if I misunderstood you but try adding a position Vector (your middle point) to pos.

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 ImTheC · Sep 07, 2016 at 06:08 AM

@Maniak101, I know this was a long time ago, but I was having difficulty figuring this out myself. What I ended up doing was this:

 // Instantiates a prefab in a circle around a specific point (shieldGen)

 public GameObject prefab;
 public int numberOfObjects;
 public Transform shieldGen;
 public float radius = 5f;

 void Start() {
     for (int i = 0; i < numberOfObjects; i++) {
         float angle = i * Mathf.PI * 2 / numberOfObjects;
         Vector3 pos = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius;
         Instantiate(prefab, shieldGen.position + pos, Quaternion.identity);
     }
 }

Then, in the Inspector, I attached an empty gameobject to the "shieldGen" wherever I wanted the objects to spawn. In my case, it was an empty gameobject attached to the player gameobject.

Hope this helps anybody else searching for this issue!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Moving the camera with mouse not working 1 Answer

Why does my sphere object stop moving on collision? 1 Answer

How do I display text in current Unity in the first roll-a-ball project? 1 Answer

Incomprehensive auto death on 2D runner with energy depleting mechanics [Debugging] 1 Answer

Downsize A 3DS Model? 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