Set a variable to the local rotation of object

I need to set a variable to the local X rotation of an object.

What i use at the moment is rotationx=transform.eulerAngles.x;

Is there another way to retreive the local rotation? Eulerangles makes a bit trouble here, flipping to 180 from time to time :slight_smile:

Hmm… transform.localRotation maybe?

Thanks ESB. That was my first thought. I don`t get an error. But the variable stays zero, unfortunately :frowning:

transform.localRotation is a Quaternion. Probably best if you track the rotation yourself.

–Eric

Peculiar. localRotation should give you at least a Quaternion. What, more precisely, are you trying to do? (I don’t have high hopes of being able to figure anything out, but have had to muck around with rotations a bit lately, so I find the topic interesting…)

EDIT: Good morning, ESB - read the whole post, maybe? Yes: picking the x variable out of a Quaternion is mostly useless. They are something akin to dark magic…

Well, i work at a little non physics based flying engine. I´m still battling with the basics of Javascript, and i battle especially with the basics of everything movement and rotation related. This flying thingie here is my testproject to learn this basics :slight_smile:

What i try to achieve is to retreive the Z rotation of the airplane. And when it is not zero i want to rotate it back to zero when the user doesn`t press left or right. This actually already works with retreiving eulerAngle.z. But this eulerAngle.z flips to 180 from time to time. And that makes trouble with especially flying a looping then. At the zenith the Z eulerangle flips from 0 to 180, and my code to rotate the airplane back starts to do his job. Which kills the looping. So i need another way to retreive the local angle. Means when there is a way :slight_smile:

This issue becomes also interesting when i add a compass. A compass usually goes from 0 to 360, and doesn`t flip at 180 degrees :slight_smile:

Hm, dunno if it makes sense to attach the whole script. It is in the middle of the process, and lacks a few essential features i want to add. And is of course surely full of mistakes :slight_smile:

In essential the current crucial part is:

	rotationz=transform.eulerAngles.z; 

	if ((rotationz >=1)  (rotationz < 180)) transform.Rotate(0,0,Time.deltaTime*-50);
	if ((rotationz >=1)  (rotationz > 180)) transform.Rotate(0,0,Time.deltaTime*50);
}

I need to get rid of the eulerAngles, or have to add two more lines of code to work around the 180 degree issue …

Just to see if I understand. You only want the airplane to rotate around the z-axis while the user is pressing left or right? As soon as user input stops, the plane should just stop rotating? I might be completely off or misunderstanding you here, but wouldn’t it suffice to just make sure you only alter/add to the z rotation while you have user input?

It`s the other way around. User presses left → rotation goes to left. BUT when user releases left → rotation goes back to right until it reaches 0 :slight_smile:

Okay, so you want it to rotate back to some set direction? Like … it’ll always be heading straight north unless you are temporarily steering it?

Exact :slight_smile:

But not facing north. I want the airplane to face upwards. Until i fly a curve. Then the airplane rotates sidewards a bit, and rotates back to straight when i release the button :slight_smile:

Just to make sure that we don`t drift away from the core problem. My problem is still the eulerAngle rotation of the object. Which flips to 180 from time to time. I need to retreive a local rotation instead, that has fully 360 degrees :slight_smile:

Yes… What I was thinking is you might not have to muck around with the 0/180 thing at all, just having some set base rotation or world-related direction that the plane should return to when there is no input… But right now, I’m afraid my mind’s drawing blank after blank. Not enough coffee input yet, I think.

That is my blank. Give um back! :stuck_out_tongue:

Thanks anyways. Then let`s try some workarounds :slight_smile:

Hehe, they are a computationally cheaper way of representing a rotation than doing it by matrix. Its almost always a scalar and a normalized vector in the form of

w,(x,y,z).

But im afraid im no good with the issue at hand, just thought i’d chip in :slight_smile:

So… I’ll keep on rambling with no clear solutions! (I am thinking of how to do something similar for a project of my own, so it’s Relevant To My Interests…)

What I’d be wanting to achieve is to make the transform straighten up after user input is done (“head” up, “feet” down, so to speak).

I think I’d be inclined to, once the user input stops, ask the transform to rotate back to a rotation where the relevant angle (up pointing in the global up-direction in my case) is 0, with something like

    myTransform.up = Vector3.RotateTowards(myTransform.up, Vector3.up, rotationSpeed * Time.deltaTime, 0);

…but for some reason I bet that doesn’t quite do what I’d want.

Can you use Vector3.Angle to get the angle between the plane’s transform.up and Vector.up (ie, the global up direction)? This won’t tell you whether the plane is rotating left or right, but you will already have needed that information to tilt the plane in the first place.

Ah, but i would need the information if it is left or right. Else i rotate into the wrong direction :slight_smile:

Thanks anyways. Have solved it by adding the two missing lines of code now instead to compensate the 180 degrees flip of the euler angle :slight_smile:

Only a bit disturbing thing is that this way my airplane rotates to upwards down when the angle is bigger than 90 degrees. But i can live with that. At least for now. I`m still a rookie, battling with the basics :slight_smile:

rotationz=transform.eulerAngles.z; 

	if ((rotationz >0)  (rotationz < 90)(!Input.GetButton ("Horizontal"))) transform.Rotate(0,0,Time.deltaTime*-50);
		if ((rotationz >0)  (rotationz > 270)(!Input.GetButton ("Horizontal"))) transform.Rotate(0,0,Time.deltaTime*50);
		if ((rotationz >180)  (rotationz < 270)(!Input.GetButton ("Horizontal"))) transform.Rotate(0,0,Time.deltaTime*-50);
		if ((rotationz <180)  (rotationz > 90)(!Input.GetButton ("Horizontal"))) transform.Rotate(0,0,Time.deltaTime*50);

A way to retreive the separated Quaternion angles would be neat :wink: