Change Object's Rotation Based on Rotation of a Prefab when Instantiated?

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.

I don’t see anything obviously wrong here. You’ll need to debug what’s happening. That can be using a debugger to stop execution at break points and inspect values, or using Debug.Log to print out values at various places and ensure they are what you expect them to be. Then you can use this to narrow down where exactly the problem stems from.

1 Like

Yeah, the thing is as far as I an tell the problem is arriving on one line, so I’m not really sure what could be happening, or how to properly debug it. I’ve tried looking at the values, but they all seem to be correct, is there something that I’m missing as to how transferring rotation from child objects works?

You’re setting the parent of the spawned bullet to be _itemPivot.transform.GetChild(0), but you’re reading off the local rotation of _bulletSpawnPoints
local rotation is relative to the parent, so that only makes sense if _itemPivot.transform.GetChild(0) is the parent of bulletSpawnPoints*, or if the absolute rotation of those two objects are the same.
I generally find it easier to just work with the forwards vector and let the engine deal with figuring out the rotations. So if the spawn points are pointing in the correct direction, I’d just replace this line:
_
*_ <em>*g.transform.localRotation = Quaternion.Euler(_bulletSpawnPoints[i].localRotation.eulerAngles);*</em> _**

with this:
*_ <em>*g.transform.forwards =_bulletSpawnPoints[i].forwards*</em> _*

Thanks for the help! I tried that and it didn’t seem to work, but then I realized something: when assigning the spread on the bullet on lines 14 and 15 I was setting the rotation of the bullet to 0 on the x and y axis’s, I fixed it now and it seems to be working fine. Thanks for the help though!