How to Spawn a wave of objects with the tip pointing at the targets direction?

What I am trying to achieve is to get the tip of the wave to point at the targets direction.

public void SpawnWave(GameObject WaveBullet, Vector3 TargetPosition, float bullets = 7)
    {

        float angle = 180f / bullets;
        Vector3 direction = (TargetPosition - this.transform.position).normalized;
        // use a loop to instantiate the prefabs around the half circle
        for (int i = 0; i < bullets; i++)
        {
            // calculate the current angle
            float currentAngle = angle * i;

            // calculate the x and z positions of the prefab based on the radius and current angle
            float x = 5 * Mathf.Cos(currentAngle * Mathf.Deg2Rad);
            float z = 5 * Mathf.Sin(currentAngle * Mathf.Deg2Rad);


            Vector3 position = new Vector3(x, 0, z);
            Quaternion rotation = Quaternion.LookRotation(direction,this.transform.up);

          Instantiate(WaveBullet, position + this.transform.position, rotation);

          
        }
    }

8670804--1168056--ezgif-3-105c8a79c3.gif

I would create an empty GameObject, parent all of the projectiles to it (positioning them locally), and then rotate the holder.

That simplifies the vector math :stuck_out_tongue: . You can unparent them afterwards and destroy the holder, if you want.

Multiplying the relative position by the same rotation quaternion you already have should do what you want.

Something similar to:

Vector3 position = rotation * new Vector3(x, 0, z);
2 Likes

Thanks! I would of never figured it out.
8672145--1168323--ai.gif