# Transform.Rotate for Quaternions?

Hullo again. Some background first.

I’m working on a turret stabilization script using independent Quaternions. It works great if the rotations are automated (ie, given a specific aimpoint and such) but now I need the player to be able to control it.

Here’s a rough and ready snippet from the code:

``````public var pivotX : Transform;
public var pivotXRotation : Quaternion;
public var pivotY : Transform;
public var pivotYRotation : Quaternion;

public var speed : float = 24.0;

function Update() {

pivotX.rotation = pivotXRotation;
pivotX.localEulerAngles = Vector3(0,pivotX.localEulerAngles.y,0);
pivotY.rotation = pivotYRotation;
pivotY.localEulerAngles = Vector3(pivotY.localEulerAngles.x,0,0);

}

function Traverse(aimpoint:Vector3) {

var targetRotation = Quaternion.LookRotation(aimpoint - pivotX.position,pivotX.up);
pivotXRotation = Quaternion.RotateTowards(pivotXRotation, targetRotation, speed*Time.deltaTime);

}

function Elevate(aimpoint:Vector3) {

var targetRotation = Quaternion.LookRotation(aimpoint - pivotY.position,pivotY.up);
pivotYRotation = Quaternion.RotateTowards(pivotYRotation, targetRotation, speed*Time.deltaTime);

}
``````

Okay, so what I’ve tried so far was to switch over to another set of codes when the player assumed control, this time using independent eulerAngle values (like the packaged Mouse Look).

Here’s a wee bit of code:

``````public var pivotX : Transform;
public var pivotXRotation : float;
public var pivotY : Transform;
public var pivotYRotation : float;

public var speed : float = 24.0;

function Update() {

Control();

pivotX.eulerAngles = Vector3(pivotX.eulerAngles.x,pivotXRotation,pivotX.eulerAngles.z)
pivotX.localEulerAngles = Vector3(0,pivotX.localEulerAngles.y,0);
pivotY.eulerAngles = Vector3(pivotYRotation,pivotY.eulerAngles.y,pivotY.eulerAngles.z)
pivotY.localEulerAngles = Vector3(pivotY.localEulerAngles.x,0,0);

}

function Control() {

pivotXRotation += Mathf.Clamp(Input.GetAxis("Mouse X"),-speed,speed);
pivotYRotation -= Mathf.Clamp(Input.GetAxis("Mouse Y"),-speed,speed);

}
``````

It works to a point, but I’m having a hell of a time and a sackload of problems trying to sync the quaternions with the new rotations when switching between them. I assume that using Quaternions exclusively should solve my problem.

So to the question, is there a way to rotate Quaternions on individual axes like Transform.Rotate? Prefereably, it should function similarly to the packaged MouseLook script but any method that works is welcomed. Thanks for the time, folks.

UPDATE: my revised working code according to robertbu:

``````public var pivotX : Transform;
public var pivotXRotation : Quaternion;
public var pivotY : Transform;
public var pivotYRotation : Quaternion;

public var speed : float = 24.0;

function Update() {

Control();

pivotX.localEulerAngles = Vector3(0,pivotX.localEulerAngles.y,0);
pivotY.localEulerAngles = Vector3(pivotY.localEulerAngles.x,0,0);

}

function Control() {

var moveX = Mathf.Clamp(Input.GetAxis("Mouse X"),-speed,speed);
var moveY = Mathf.Clamp(Input.GetAxis("Mouse Y"),-speed,speed)

currentPosition.pivotXRotation = currentPosition.pivotXRotation * Quaternion.AngleAxis(moveX,currentPosition.pivotX.up);
currentPosition.pivotYRotation = currentPosition.pivotYRotation * Quaternion.AngleAxis(-moveY,currentPosition.pivotY.right);

}
``````

Have you looked at Quaternion.AngleAxis()?