What I’m trying to do is instantiate multiple projectiles based on one rotation, and slightly adjust the rotations of the projectiles through iteration to create a ‘cone attack’ effect. However, it only works well when the target is to the right of the origin/attacking unit. The rotations of the projectiles become tighter and tighter the further left the target is, until they’re all overlapping when the target is directly left of the origin. Any help or insight would be greatly appreciated.
for (var i = 0; i <= NumberOfPulses; i++)
{
var didCenter = false;
for (var n = 1; n <= BulletsPerPulse; n++)
{
if (n == 0) continue;
_playerPos = _playerTransform.position;
_myPos = _myTransform.position;
// rotation toward player -->
var diff = _playerPos - _myPos;
diff.Normalize();
var rotZ = Mathf.Atan2(diff.y, diff.x)*Mathf.Rad2Deg;
var newRotation = Quaternion.Euler(0f, 0f, rotZ);
var newRot1 = newRotation;
var newRot2 = newRotation;
// <--
newRot1.z += (BulletSpreadVariance*n);
newRot2.z -= (BulletSpreadVariance*n);
bullet1 = Instantiate(Projectile, _myPos, newRot1) as GameObject;
bullet2 = Instantiate(Projectile, _myPos, newRot2) as GameObject;
if (!didCenter)
{
bullet3 = Instantiate(Projectile, _myPos, newRotation) as GameObject;
if (bullet3)
{
bullet3.rigidbody2D.AddForce(bullet3.transform.right*BulletSpeed);
bullet3.name = n + " " + newRotation + " " + " bul1 " + (BulletSpreadVariance*n);
Destroy(bullet3, BulletLifetime);
didCenter = true;
}
}
if (!bullet1 || !bullet2) continue;
bullet1.rigidbody2D.AddForce(bullet1.transform.right*BulletSpeed);
bullet2.rigidbody2D.AddForce(bullet2.transform.right*BulletSpeed);
bullet1.name = n + " " + newRot1.z + " " + " bul1 " + (BulletSpreadVariance*n);
bullet2.name = n + " " + newRot2.z + " " + " bul2 " + (BulletSpreadVariance*n);
Destroy(bullet1, BulletLifetime);
Destroy(bullet2, BulletLifetime);
}
}