- Home /
Quaternion.LookRotation with an offset.
Hi everyone, this is my script :
function Update () {
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit) && !Input.GetMouseButton(1) && !Input.GetMouseButton(2) && hit.rigidbody && PartPlacement.gui == false) {
transform.position = hit.point;
transform.rotation = Quaternion.LookRotation(hit.normal);
}
transform.eulerAngles.z = PartPlacement.rotation;
}
I am trying to make a rotation offset for Quaternion.LookRotation using eulerAngles. The code that I have so far does its job perfectly until the transform is aligned with y axes. Then instead of being an offset static float "rotation" becomes a rotation speed value or in other words instead of degrees it becomes revolutions per second. I need Quaternion.LookRotation to stay in the script if possible.
It's fixed!
Full code:
function Update () {
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit)) {
transform.position = hit.point;
transform.rotation = Quaternion.LookRotation(hit.normal) * Quaternion.Euler(0, 0, PartPlacement.rotation);
}
}
From the reference:
Do not set one of the eulerAngles axis separately (eg. eulerAngles.x = 10; ) since this will lead to drift and undesired rotations. When setting them to a new value set them all at once as shown above.
Reading and using eulerAngles this way is dicey. There are multiple eulerAngle representations for any given physical rotation, and the one you put in is not necessarily the one you get back out.
Yeah, I googled (before posting this) and also found that setting axes separately in eulerAngles is no good, but I still could not fix the problem by my self, this is just as close as I can get... Can someone post a fixed script? I really have no idea what to change, still new to both Quaternion and eulerAngles.
Answer by gfoot · Apr 28, 2014 at 08:17 PM
Multiply the quaternion by an Euler rotation quaternion like this:
transform.rotation = Quaternion.LookRotation(hit.normal) * Quaternion.Euler(0, 0, zAngle);
You can also specify a custom "up" axis to LookRotation, in case that suits your inputs better.