Relative angle between two vectors problems with sign

Ive read tonns of topics regarding aligning to normal e.t.c. but it didnt help me)
I need to know a relative angle between two vectors (its three components in euler). Please see picture. Green ray is vGravityDir and red one is (-transform.up). On picture is shown a desired result (angle drawn is for relative to objects local x axis( or RelativeGravityEulers.x in code) )
The problem is that sign of angle is always the same for pic1 and pic2.

What i`ve tryed:

		Vector3 RelativeGravityEulers = Quaternion.FromToRotation(-transform.up, vGravityDir).eulerAngles;
		Quaternion RelativeShipDir = Quaternion.FromToRotation(-transform.up, Vector3.down);		
		RelativeShipDir *= Quaternion.FromToRotation(vGravityDir, -transform.up);		
		Vector3 RelativeGravityEulers = RelativeShipDir.eulerAngles;
        Vector3 directionInEulers = Quaternion.LookRotation(vGravityDir).eulerAngles;
        Vector3 RelativeGravityEulers = ClampHeading(directionInEulers) - ClampHeading(transform.eulerAngles);

    public static Vector3 ClampHeading(Vector3 eulers)
    {
        for (int i = 0; i < 3; i++ )
            eulers[i] = ClampHeading(eulers[i]);
        return eulers;
    }

    public static float ClampHeading(float heading)
    {
        if (heading > 180f)
            return heading - 360f;
        if (heading < -180f)
            return heading + 360f;
        return heading;
    }

This all works except the for sign, for example axis RelativeGravityEulers.x dont change sign and show the same angle for two situations shown on pics.

What am I doing wrong?


You probably don’t want to use Euler angles for this. Can you explain the actual problem you’re trying to solve? (That is, what are you trying to get the object in question to do?)

I want to use angles as input for PID controller (for every axis separately) to use with AddTorque.
This is the same problem as we talked on UnityAnswers but there I didn`t come to what I want and since that time tryed to solve problem myself. Anyway thank you)

I don’t get it. I think you might be overthinking this problem. By the way, you can use Vector3.Angle to get an angle between 2 vectors.
If you want to get the sign of an angle between 2 vectors, you need to provide some other reference. Like say the forward vector of your ship. Then you can take the sign of the Dot product of this forward vector and the gravity vector and this will be the sign of your angle. (assuming all 3 vectors are in the same plane).
As Jesse suggested, you can try to explain what is your end goal. You might be going at it the wrong way.

An angle between two 3d vectors doesn’t have a sign. Signed euler angles require a 2D context (the plane defined by the two vectors), a reference vector that indicates 0 degrees and a convention for which side is + and which is -.

For the specific problem in your pictures, your reference vector appears to be the red line and the forward vector of your vessel appears to determine the sign.

From the top of my head (no testing or anything), your sign should be given by:
sign = Mathf.Sign(Vector3.Dot(Vector3.Cross(Vector3.Cross(red, green), red), forward))
Where red is your base vector, green the vector you’re comparing to and forward the forward of your vessel.

I’m quite confident that this formula will work, although it may have + and - the wrong way around.

Edit: beaten by ivkoni. However, my formula should also work if your 3rd vector is not in the same plane.

Tryed Vector3.Angle, result is the same. Really, maybe I should determine the sign manually, will try.
End goal: stabilize ship in X and Z axes with respect to gravity fully using physics(with no freeze rotation checked etc), so using PID controllers for that axes to count AddTorque. The input parameter for PID controller should be relative angle between gravity direction and x and z local axes of ship, and target is angle of 0 now and later some other. And this must be achieved with variable direction gravity. With gravity Vector3.down everything works OK.

This

works great, thank you!
Left to understand how it works, but, I hope, I will =)