0
\$\begingroup\$

Here, I have a plane model with some sub parts(like aileron) can rotate:

enter image description here

That red section is weight painted(rigged) vertices, and I need to control the rotation of this in Unity by script.

So this is the image in Unity, rotating through green ring(along x axis) of aileron bone from gizmo works exactly I want.

enter image description here

However as you can see in the inspector, I rotated along X axis but it doesn't changed only x axis, it also changed other axis like Y and Z.

So when I just rotate X rotation of aileron from the script or inspector, it gives me total wrong rotation, and here is the solution I got: How to rotate object just like in the scene?

So I wrote the code:

leftAileron.Rotate(0, value, 0);

Now it rotates desired angle but the problem is transform.Rotate method is just rotating the object, means adding the new value to current rotation.

In my case, I need to "set" the rotation, not adding it, because it gives wrong rotation in this case.

I made a slider for explain and demonstrate my problem: here is the slide and it's range is -5 to 5 and default is 0.

enter image description here

The code for rotating aileron is this:

sliderLeftAileron.onValueChanged.AddListener((value) => {
    leftAileron.Rotate(0, value, 0);
});

When I moved slide to left, value goes negative so it rotates like this.

enter image description here

But when I slightly moved the slide to right, it still rotate same direction because the value is still negative and just try to adding it.

enter image description here

So I need to set the rotation of aileron, not adding it, moving slide to right should moves opposite direction and vise versa.

Is there a way to set rotation just like Rotate(0, value, 0) but not adding it in this case, like this:

transform.rotation = Quaternion.Euler(0, value, 0);

I tried bunch of ways but none of them worked, how do I treat my aileron rotation just like in gizmo in the scene?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I'd recommend doing something like this:

public class LocalRotator : MonoBehaviour {

    // Let the inspector / other scripts control how far to rotate this, 
    // and about which local axis.
    public Vector3 localAxis = new Vector3(0, 1, 0);
    public float angleChange = 0f;

    // Cache your local rotation just after the object is loaded/enabled in the scene.
    Quaternion _initialOrientation;
    void Start() {
        _initialOrientation = transform.localRotation;
    }

    // Each frame (or in response to slider change events if you prefer)
    // update the local rotation as the composition of the input value
    // applied relative to the original orientation.
    void Update() {
        var change = Quaternion.AngleAxis(angleChange, localAxis);
        transform.localRotation = _initialOrientation * change;
    }
}
\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .