[Solved]transform.rotation based on previous rotations

Hello,

This is a very difficult question to explain so if it is not clear please tell me.

I have a flying game were I rotate an object in z axis to roll it and x for pitch (up/down). My code is such:

transform.localEulerAngles = new Vector3(ElevatorAngle, 0, -RollAngle);

The angles are calculated on the fly by the positions of fingers on the screen and thats why I dont use Rotate because I want to set it to a particular rotation.

The problem is when the aircraft has rolled to one side you would want it to use up pitch so the plane can turn, flight sim people will know instantly what I mean.

The problem is the way I feed in x and z rotation angles. This seams to prevent rotating in x, and then y based on the previous rotation (x).

You can test this in Unity by rotating something in x with the widget and then in z after the previous rotation. You will see in the object transform some interesting rotations come up.

I cannot seem to be able to recreate this in-game. This is a commercial project and I really need the help!

Thank you for your time!

EDIT: Basically how can I combine the rotations?

EDIT: Has been fixed thanks to Spreos, here’s the final code:

        //Make Rotations
		Quaternion Pitch = Quaternion.Euler(ElevatorAngle, 0, 0);
		Quaternion Roll = Quaternion.Euler(0, 0, -Angle);
			
		//Rotate Character
		transform.localRotation = Roll;
		transform.localRotation *= Pitch;

If you want to “rotate” a rotation based on a new given local rotation, you can multiply the two rotation’s quaternions (you can also use a vector * quaternion multiplication so it rotates the vector from it’s current position).

Remember not to define the quaternion’s components itself, because there’s a lot of math involved in that, and it’s not easy to calculate it by yourself. You should define the rotation quaternion you want to apply with the Quaternion.Euler() method.

Not sure if it was that what you were asking for, hope it helped ^^