Recently I’ve been trying to develop a shotgun mechanic for my top-down shooter game. I have a list of transforms for where to spawn each bullet relative to the gun. And my code goes as follows:
foreach (Transform t in _bulletSpawnPoints)
{
GameObject g = Instantiate(_currentGun.gunData.bullet, _itemPivot.transform.GetChild(0), false);
g.transform.localPosition = _bulletSpawnPoints[i].localPosition;
g.transform.localRotation = Quaternion.Euler(_bulletSpawnPoints[i].localRotation.eulerAngles);
if (_currentGun.gunData.bulletSpeed != 0)
{
Bullet bullet = g.GetComponent<Bullet>();
bullet.speed = _currentGun.gunData.bulletSpeed;
}
var r = Random.Range(-_currentGun.gunData.spread / 2, _currentGun.gunData.spread / 2);
g.transform.localRotation = Quaternion.Euler(0, 0, g.transform.rotation.z + r);
i++;
}
For some reason, on line 6 it isn’t assigning the correct rotation, and is instead assigning a rotation that is much smaller than the desired one. For context my shotgun has 3 bulletSpawnPoints, each with the respective rotations of 20º, 0º, and -20º. But when the bullets spawn, they have the correct position, however the rotations are always around 0.1 degrees apart, instead of 20 degrees like they should be. I’m not really sure what’s causing this, but I don’t understand Quaternions well enough to be able to figure it out. I’ve tried every solution that I can find to this, and similar issues, so if anybody has the solution to my problem I would be very happy if you could share it.