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 SovietAlex · Jul 24, 2018 at 07:10 PM · rotationinstantiatecanvascirclecircular

Instantiate an image, equally in a circle and rotate it away from the center?

I want to create bullets around the gun so that they are in a circle. And rotate them away from the gun. I can do this manually but I want it to be automatically done by code.

And the order of the bullets has to start from the top and go to the right.

I appreciate any help I can get.

This is what it looks like manually

alt text

alt text

pistol.jpg (49.4 kB)
ak47.jpg (69.0 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

2 Replies

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

Answer by SovietAlex · Jul 30, 2018 at 03:57 AM

I figured it out myself. Here is the code if anyone ever needs it in the future.

 float totalBullets =  30; //Total Images To Create
 float radius = 50; //How Far The Image should be away from the center
 Transform CenterPos = GunImage.transform; //The Center Position"
 
 for (int i = 0; i < totalBullets; i++)
 {
       float point = i / totalBullets;
       float angle = (point * Mathf.PI * 2);
       float x = Mathf.Sin(angle) * radius;
       float y = Mathf.Cos(angle) * radius;
       Vector3 pos = new Vector3(x, y, 0) + CenterPos.position;
       Image myImage = Instantiate(BulletImage, pos, Quaternion.Euler(0, 0, -Mathf.Rad2Deg * angle, CenterPos);
 }

And then if you need to change the size of the image through code you can use this

 myImage.rectTransform.sizeDelta = new Vector2("Width", "Height");

And if you want to drag and drop the Image into the Inspector. You have to create a Prefab that has an Image Component and then you have to select the image you want in the prefab.

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 toddisarockstar · Jul 27, 2018 at 03:16 AM

the easiest way i can think of is to use a parent object in the center

 GameObject pivot;
     GameObject [] bullets;
     float space;
     int amount;
     void Start () {
         amount = 12;
         space = (float)360 / (float)amount;
 
         pivot = new GameObject ();
         bullets = new GameObject[amount];
         int i = amount;
         while(i>0){i--;
             bullets[i] = GameObject.CreatePrimitive(PrimitiveType.Cube);
             bullets[i].transform.position = new Vector3(pivot.transform.position.x,
                                                         pivot.transform.position.y+6,
                                                         pivot.transform.position.z);
             bullets[i].transform.parent = pivot.transform;
             pivot.transform.Rotate(0,0,space);
 
         }}
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 haidao0923 · Jul 27, 2018 at 09:17 PM 0
Share

I would like to ask why the script worked with "while(i>0)" and not when I do "for (i;i>0;i--)"?

avatar image toddisarockstar haidao0923 · Jul 28, 2018 at 12:38 AM 0
Share

you should be able to do the same with a for loop. Sorry, ive just been using "while" a lot lately for other reasons. just replace line 12 with :

 for ( i = 0; i < amount; i++){

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

144 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

Related Questions

Objects rotator (rotate objects around a circle) 2 Answers

Rotate a Canvas element over time 0 Answers

Clone positioning inside loop 1 Answer

How to rotate an Instantiated object? 1 Answer

Dividing the circular rotation into parts? 2 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