Rotate a quaternion variable

I just managed to get weapon spread working. However, I feel like I made such an ugly hack that I ought to ask how this should be done in a more clean way. Lots of text for what I hope is a simple question. :slight_smile: Sorry for that.

What I essentially would like to do is to rotate a rotation variable. Which I don’t know how to do. The code below is from my revolver code (this happens when a bullet is fired). The “spread” value is a float set in the editor to specify the maximum inaccuracy of a bullet. On my revolver I have a child gameObject called “bulletSpawn” (basically the barrel tip of the gun). In my current code, I rotate this child gameObject, spawn the bullet on it, and then rotate it back(!). Which works. However, I would much rather just read the rotation of the bulletSpawn object, assign that value to a local Quaternion variable, and the rotate THAT variable (and use it on the last line for the instantiation).

Something like:
Quaternion spawnRot = bulletSpawn.transform.rotation;
spawnRot.Rotate(new Vector3(inaccuracy,0,0); // ← This is not allowed

This is my current code (working, but ugly):

			Vector3 spawnPos = bulletSpawn.transform.position;
			
			//Strange hack for spread (should not really be needed):
			
			float inaccuracy = Random.Range(-spread,spread);

			//Rotate the spawner a random value
			bulletSpawn.transform.Rotate(new Vector3(inaccuracy,0,0) );
			//Read the spawn rotation
			Quaternion spawnRot = bulletSpawn.transform.rotation;
			//Then rotate the spawner back again.
			bulletSpawn.transform.Rotate(new Vector3(-inaccuracy,0,0) );
			
			PhotonNetwork.Instantiate("Bullet38", spawnPos, spawnRot, 0);

EDIT: The gameplay is in 2D, that’s why I only need to rotate along one axis.

I’d place something like this in the awake function of the bullet, and then just assign a random rotation in there. So maybe it would look like:

var inaccuracy : float;

function Awake () {

inaccuracy = Random.Range(-spread, spread);

transform.eulerAngles.x = inaccuracy;

}

Straight from the docs:

You can use Quaternion.operator * to rotate one rotation by another, or to rotate a vector by a rotation.

JohnnyA. Thanks a lot for pointing me in the right direcation. Can’t believe I had missed that. Very good to know(!). :wink:

Posly. Yes, in most cases I agree that would be the best, most straight-forward solution. However, in my case, the bullet is spawned over the network, and I have a great opportunity during the actual instantiation call, to specify its position and rotation. Since the Awake-function will happen locally on all clients, that would mean the bullet would have different angles on different clients if I set it in the awake funciton.

Thanks a lot, both of you. Code looks a lot better and less hack-ish now.

Re-written code:

		if(photonView.isMine)
		{
			Vector3 spawnPos = bulletSpawn.transform.position;
			
			float inaccuracy = Random.Range(-spread, spread);

			Quaternion spawnRot = bulletSpawn.transform.rotation;
			spawnRot *= new Quaternion(inaccuracy * Mathf.Deg2Rad, 0, 0, 1);
			
			PhotonNetwork.Instantiate("Bullet38", spawnPos, spawnRot, 0);
		}