- Home /
Spawn objects in a circle with an even spacing in between?
Hi guys, this is my first question here and I am just starting out coding C# in Unity. I am currently coding an enemy that will fire projectiles in 360 degrees, but with the current code, the projectiles will have a uneven amount of spacing in between. What should I change in order to make it have an even spacing in between each projectile? Thanks in advance!! I have attached an image to show what the issue is.
float angle;
public int pieceCount;
public int radius = 1;
public GameObject projectile;
private void Start()
{
angle = (Mathf.PI * radius) / pieceCount;
for (int i = 0; i < pieceCount; i++)
{
float x = Mathf.Cos(angle) * radius;
float y = Mathf.Sin(angle) * radius;
Vector2 pos = new Vector2(x, y);
Instantiate(projectile, pos, transform.rotation);
angle++;
}
}
Answer by tormentoarmagedoom · May 16, 2019 at 07:53 AM
Hello @Komamon0801
I see you are instantiating the projectiles, nut i do not understand how the projectiles move.
I also dont know why the space is uneven... maybe is because Cos and Sin are not lineal...
Well, what i can recommend you is this:
-To instantiate the projectiles, use an empty gameobject (a transform) as a anchor or reference to spawn the projectiles (ets call it SpawnPoitn). Then you move this Spawnpoint following a lineal circular move... how?
-Its easy. Imagine you have another empty gameobject (called Rotator). Then you make the Spawnpoint child of this Rotator, but not place it at 0,0,0, you make the Spawnpoint to have a localcposition (relative to rotator) of (5,0,0).
-If you now just rotate this Rotator, the spawnpoint will move in a circular path of radius 5.
-So, controlling the rotation with eulerAngles of this Rotator, you can have a control of how much degreess is moveing, so you can make even stops in this rotation to spawn projectiles from SpawnPoint.
Was clear? If need more help, just ask!
Bye! :D
Your answer
Follow this Question
Related Questions
Making a circle and input multiple images. 2 Answers
Network spawning not working 0 Answers
Client can't send Commands despite localPlayerAuthority is true 0 Answers
Problem with instantiating the object in untiy at certain time intervals 1 Answer
My spawned obstacle stop working properly after a certain time 1 Answer