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
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 *= GameObject.CreatePrimitive(PrimitiveType.Cube);*
_ bullets*.transform.position = new Vector3(pivot.transform.position.x,_
_ pivot.transform.position.y+6,_
_ pivot.transform.position.z);_
_ bullets.transform.parent = pivot.transform;
pivot.transform.Rotate(0,0,space);*_
* }}*
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.