How can I make my airplane pitch guage work correctly? (x rotation translated to y position)

I am trying to make a gauge that can measure my the x rotation of my plane. So far I have it so that it does move the gauge up and down in the UI but I’m having trouble getting it to work correctly.

Here is what I have so far:

    public float yPos;
    public Transform target;
    void Update ()
    {
        //What do I need to change this to?
        yPos = target.transform.eulerAngles.x;
        //The code below is working fine
        Vector3 newRotation = new Vector3(0,yPos,0); //translate the new x rotation to y movement
        this.transform.localPosition = newRotation; // move the gauge up and down
    }

What I need to have happen is some way to tell EXACTLY where the planes nose is pointing.

The problem I have been having is that when it rotates along the x axis, it gives these readings

up (sky)
270 to 360 to 270

down (ground)
0 to 90 to 0

What I need it to do is not come back down again and keep going.

The end result is that I need to have some way to make my gauge work like the scrolling background in the space shooter tutorial but with its movement controlled by the planes X rotation and not time.

PS using normal rotation proves tricky because it does not record the x correctly when the planes y changes.

PLEASE HELP ME ( ; - ; )

euler angles are just bad in general in 3-space as they are dependent on the ordering of the other axes.

Instead measure your actual pitch.

If you use the default forward (z) for your forward in your model. Then get the y portion of your forward vector, this is you pitch.

sin(theta) = forward.y

If you want it as an angle

theta = asin(forward.y)

var pitch = Mathf.Asin(transform.forward.y);

Note, that’ll be in radians, not degrees. So you’ll get values from -pi/2 to pi/2 (-90 to 90), where 0 is level, -pi/2 is pointing straight down, and pi/2 is pointing straight up.

You’ll probably want to scale that value if you use it in a gauge… not sure what your gauges scale is though.

1 Like

That doesn’t seem to fix it. although it shows the negative and positives, it still has the same issue of going up and then down once it passes 90 degrees. i need some method that does not do that. that makes the guage to reverse back when the plane is pointing up or down.

I think i might be able to use this but if there is some way that i could differentiate between pointing past up and down? It would be perfect if i could somehow. It would be okay if there was some variable that could differentiate between these two.

Technically speaking that’s accurate for the pitch, because once you pass 90 you start reapproaching level flight… just upside down.

If you want that, you’re going to have to take into consideration your up vector. You can take the arccosine of the y portion of your up vector. This will give you your pitch as a measure from 0->pi (like you want), but without direction (no +/-). But you can multiply it by the sign of your forward measure pitch to get both.

var pitch = Mathf.Acos(transform.up.y) * Mathf.Sign(Mathf.Asin(transform.forward.y));

The downside to this though is that if you roll at all, you’ll have to remove that as well, so it isn’t perfect.

If we want to ignore any roll, you’ll probably have to define exactly how you want pitch measured… if it’s always off the ‘world up’, you could just get the angle between the ‘world up’ and the forward. But we need information on what you plan to display as pitch in this scenario.

This is the downside of euler… measuring a single pitch, yaw, or roll is always dependent on the conditions of the others. It’s why unity uses quaternion’s for rotation, and only converts to euler’s as needed.

1 Like

I will try that on the next iteration of my project, for now thanks for the Asin function! It still has the annoying bump but for now it can at least tell if the plane is pointing towards the ground or up at the sky. I should have also mentioned that the plane auto corrects the roll so it really only has to worry about the x and y rotation. I’m not sure if that was what you were talking about.