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. 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.