Rotate prefab around 3D object

Hello,

I have a player object and I would like to spawn some prefabs around him (some orbs) while also doing in a manner where all orbs would be evenly spaced.

I can’t figure out how to evenly space them AND also be able to speed them up bit by bit, while I increase their count as well.
The idea would be to increase the overall rotation speed while I increase their count as well.

  • the holder.position is a Transform I have to be the pivot for the rotation (linked to the player)
  • fireballOffset so that my orbs doesn’t spawn on top of the player but with a set distance from it

Here is the code I’m working with:

void Update()
{
    timer -= Time.deltaTime;
    if (timer < 0)
    {
        timer = timeBetweenSpawns;

        if (objectSpawned < maxSpawnAmount)
        {
            objectSpawned++;

            rot = (360f / maxSpawnAmount);
            GameObject orb = Instantiate(fireballPrefab, holder.position + fireballOffset, Quaternion.Euler(0f, 1f, 0f), holder);
        }            
    }

    holder.rotation = Quaternion.Euler(0f, holder.rotation.eulerAngles.y + (rot * Time.deltaTime), 0f);
}

4 Answers

4

You will need to keep track of the currently spawned orbs, namely just via a list. When you spawn a new one, you add it to the list, and can then reposition all of them locally based on how many there are.

Speeding them up is easy, just increment some value as you spawn orbs and use that to modify your rotational delta.

As for the speeding, the challenge here is that for a constant speed it becames easier since I only have to divide 360 by the number of orbs I want. For exemple, lets say I want 6 orbs. That would suggest that I set the rotation speed to 60, so that they will spawn evenly.
But what if I want even spacing but a faster rotation speed? That is my challenge.

Can you elaborate more on your list suggestion? Maybe that can help me out.

The speed and spacing are two entirely separate things. For the speed you only need to increase the delta at which you rotate the holder.

Not sure what to elaborate on the list part. Have you used List's in C#? If not, they are crucial learning. You spawn an orb, add it to the list, then run through the list and update the position of each orb.

Still not quite managed to implement it.
I moved on from this kind of weapon to come back later to it.