- Home /
[2D] Rotations become tighter the further left they are?
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);
}
}
Answer by aljovidu · May 01, 2014 at 06:34 AM
StarManta on the forums was able to enlighten me that I am misusing quaternions, and he helped point me in the right direction. You can view the thread here: http://forum.unity3d.com/threads/243575-2D-Rotations-become-tighter-the-further-left-they-are?p=1611741&posted=1#post1611741
In short, the answer was to not adjust the .z value directly, but instead modify the rotation using the AngleAxis method of the quaternion helper class.
var newRot1 = newRotation * Quaternion.AngleAxis(newRotation.z + n * BulletSpreadVariance, Vector3.forward)
var newRot2 = newRotation * Quaternion.AngleAxis(newRotation.z - n * BulletSpreadVariance, Vector3.forward)
instead of
var newRot1 = newRotation;
var newRot2 = newRotation;
newRot1.z += BulletSpreadVariance * n;
newRot2.z -= BulletSpreadVariance * n;
Your answer
Follow this Question
Related Questions
What causes this object to re-rotate itself after it hits it's destination? 1 Answer
How do I make a 2D sprite smoothly rotate upright? 1 Answer
How do I rotate a 2D object based on it's velocity? 1 Answer
Trying to rotate a 2D image over time to a specific angle, but it's rotating its Y value every time 1 Answer
Help with rotating a sprite with mouse. 2 Answers