- Home /
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);
}
Answer by robertbu · Nov 23, 2014 at 07:14 AM
Have you looked at Quaternion.AngleAxis()?
It's amazing how I missed that! Couldn't they have named it something like Quaternion.Rotate? xD
Thanks, robert! I managed to implement Quaternion.AngleAxis within a few $$anonymous$$utes and also cleaned up the rest of my code. It works great. Brilliant!
Getting a new problem, I think. $$anonymous$$y controls for pitch get stupid and sluggish when I look global left or right. They then invert when I look global backward. Any idea how to deal with this?
nvm, I fixed it by following this thread:
Your answer
![](https://koobas.hobune.stream/wayback/20220613172234im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
4 rotations WTF 1 Answer
Look Behind Me While Still Moving Forward 1 Answer
Transform.Rotate() problem : Avoid Z-axis rotation ? 0 Answers
Rotation Jumping values (0 to 180) 1 Answer