- Home /
How can I multiply the rotation of a mirrored object?
Hi everyone,
I am currently working on a mechanic in which I have an object that mirrors it's rotation to another object. The rotation of the mirrored object object get multiplied by a variable number and can also be below one. This makes a problem however.
Lets say the multiplier has a value of 0.5. This means that if the main object has a rotation of 180 degrees on a specific axis, the mirrored object has 180 x 0.5 = 90 degrees on that same axis. This works as intended, but I get against a hurdle when the rotation rotates higher than 360 degrees. Because Unity sets the next degrees of 360 to 0 instead of 361, the mirrored object will multiply 0 x 0.5 = 0 instead of 361 x 0.5 = 180.5 which causes the problem that is snaps to another unintended angle.
Does anyone have any experience in solving this issue? There probably is an easy fix that I haven't thought of ;)
Thanks!
Answer by VadimMinsk · Mar 02, 2020 at 02:55 PM
I would suggest using deltas instead of absolute angle, if possible. Here's an example that takes delta motion per frame and applies it to another transform.
public class RotatorRepeater : MonoBehaviour
{
public Transform Mirrored;
public float Ratio = 1.0f;
Quaternion LastRotation;
private void OnEnable()
{
LastRotation = transform.localRotation;
}
void Update()
{
float angle;
Vector3 axis;
Quaternion DeltaRotation = Quaternion.Inverse(LastRotation) * transform.localRotation;
DeltaRotation.ToAngleAxis(out angle, out axis);
if (Mirrored)
{
Mirrored.transform.localRotation *= Quaternion.AngleAxis(angle * Ratio, axis);
}
LastRotation = transform.localRotation;
}
}
Thanks!
This works as I needed. I probably should read into deltas a bit more ;) Have a nice day!