# Reseting rotation of a random turret

I’m trying (and failing) to make a turret-like thingr that lobs a ball in a general forward direction.

I have a shoot function that is called every few seconds. It rotates the turret to a random range of degrees (-10,10), and then shoots. Obviously, when it shoots more than once, the rotations are “added” together (if the first random number is 9, and the second is 8, it ends up rotated 17 degrees).

I need a way to save the original rotation, and rotate the turret back to it after every shot. I tried making a start function to save the position, but it didn’t seem to be working. here are the important parts of the script:

``````function Start()
{
//Saves original rotation

var startRotate = Quaternion;

}

function Shoot()
{

//rotating barrel

var x = Random.Range(-10,10);
var y = Random.Range(-10,10);
var z = 0;

transform.Rotate(x,y,z);
print("rotated to shoot");

//shooting
var ball = Instantiate(ballPrefab ,SpawnPoint.transform.position , Quaternion.identity);

//rotating back
var startx = startRotate[0];
var starty = startRotate[1];
var startz = startRotate[2];

transform.Rotate(startx,starty,startz);

}
``````

What is wrong here? It should, in theory, rotate to a random place, shoot, and rotate back a fraction of a second later, but it’s not. It just stays in its rotated position until the shoot function is called again.

Thanks,

transform.rotation is a Quaternion, a strange creature with four dimensions, none of which corresponds to the familiar angles X, Y and Z we can see in the Rotation field in the Inspector (these are actually the local Euler angles ). You should save the transform.rotation in a variable outside any function, than restore it after shooting in the shoot function:

``````  private var startRotate:Quaternion;

function Start(){
startRotate = transform.rotation;
}

function Shoot(){
// rotating barrel
var x = Random.Range(-10,10);
var y = Random.Range(-10,10);
var z = 0;
transform.Rotate(x,y,z);
print("rotated to shoot");
//shooting
var ball = Instantiate(ballPrefab, SpawnPoint.transform.position,  Quaternion.identity);