- Home /
Setting the roll of a rotation
This post (https://answers.unity.com/questions/416169/finding-pitchrollyaw-from-quaternions.html) talked about how to find the roll (and pitch and yaw) from a quaternion.
I need to do this opposite: how to SET the roll? For example, suppose I want a plane with a transform.rotation orientation to be allowed to pitch up and down, and yaw around the y-axis, but I want the roll to half every second (to give the feeling of it naturally aligning itself over time).
(I tried setting the transform.up
to slowly drift towards Vector3.up
, on each frame to ensure the plane is not rotated about the Z Axis, but of couse this also sets the pitch to zero too, which is not the intention.)
Answer by GregoryFenn · Oct 31, 2021 at 07:21 PM
I have a partial solution that isn't quite what I wanted but might be useful to people looking later.
My idea is to have a pair of gameobjects like a wing or a hand on the left and right of the main object as children. Then Unity can easily find the transform.position.y
of these two wings. If it's 0, the plane has zero roll; if it is positive, the roll is between 0 and Pi; lastly if it's negative, the roll is between 0 and -Pi (maybe the other way around but you get the idea).
So we can use
float RollMeasurer = RightWing.position.y - LeftWing.position.y;
with this operation:
this.transform.Rotate(0, 0, SomeScaler * Time.deltaTime * RollMeasurer, Space.Self);
and this code will gradually (per Update()
or FixedUpdate()
), flatten out the roll of this.transform
.
Don't know if this is suitable for your need but if I need to align anything to a specified rotation I usually use some reference transform.rotation to give the target rotation and then use Quaternion.RotateTowards inside Update to gradually align the rotation with the target.
https://docs.unity3d.com/ScriptReference/Quaternion.RotateTowards.html
That way you don't need to care about direction (always the shortest path) and need only one reference
Hope you get it working
Your answer
Follow this Question
Related Questions
Problems getting PID system and rotations to work together. 0 Answers
Get pitch and roll 0 Answers
Yaw, Pitch, Roll adjustments relative to object? 1 Answer
pitch yaw roll user input on an object? 2 Answers