- Home /
Unable to efficiently read rotation euler values as feedback
Hello, I have been trying to make a vr game where you tighten bolts by rotating a wrench around them. I had no idea how intricate rotations really are. I'm tring to make it so a bolt only rotates on its local x axis to follow a hand. I accomplished this using hinge joints and the appearance is great. The issue comes when measuring the turn so only clockwise motion is effectively tightening a bolt. I assumed it would be as easy as "when x rotation increases (righty tighty) tighten the bolt (represented by a health bar for the bolt) and when it decreases (lefty loosey), loosen the bolt (replenish health)."
I discovered that in the editor, when a hinge joint goes past 180, it loops back to 0 so I simply said if the absolute value of the rotation since last update is above arbitrary value, subtract 360 to have it represent the visual change in degree and vice versa.
my code to accomplish this is as follows:
[Range(0f, 100f)]
public float health = 100f;
float prevRot;
float rotChange;
float degToHealth = 360f / 100f;
private void Awake()
{
// Put in some starting value
prevRot = transform.localRotation.eulerAngles.x;
}
void Update ()
{
rotChange = transform.localRotation.eulerAngles.x - prevRot;
Debug.Log(transform.rotation);
// Handles any issues with rotation rolling around to 0 when reaching 360
if(rotChange > 300 || rotChange < -300)
{
rotChange = rotChange > 300 ? rotChange - 360 : rotChange + 360;
}
// remove health as necessary
health -= rotChange / degToHealth;
// bind the health
if (health >= 100)
health = 100;
else if (health <= 0)
health = 0;
// store the previous rotation to use in the next fixed update loop
prevRot = transform.localRotation.eulerAngles.x;
}
My issue is that when I try to read these values and represent the health, they are different when reading them from the transform and what is displayed in the editor.
For example the editor reads rotation of my "wrench" as (152, 0, 0), but reading from transform.localRotation.eulerAngles.x gives me (38, 180, 180). I understand it is flipping the other two axes, and showing the distance from even ground, but why? Why is it different from what is shown in the editor? and how do I correct for this when making my calculations for health?
Link to gif of issue (couldn't post a longer gif without losing legible framerate and quality) https://plus.google.com/photos/photo/112004969329323323975/6394542961768124482?icm=false&authkey=CNX8s5Pqr7_iEw
Hi, Unity stores rotations as quaternions. The euler angles values are just one representation of this quaternion in many possibles representations. I mean, there are usually multiple combinations of euler angle that can represent the same rotation. Unity is quite consistent in it's way of converting one into the other but it's always something one must be aware of (and use quaternions as much as possible)
The other complexity is the local or global (world) space. In your example, you compare the angles from the inspector that are in world space with the angles from the local space of a transform (= the rotation from it's parent object point of view, not from the world point of view). I'm sure if you display transform.rotation.eulerAngles you'll have the same values as the inspector.
For your problem, it may be easier to compare the angle in the local space as the rectangle object is rotating around 1 aligned axis (it's parent's transform's x axis i guess from the gif).
Your answer
Follow this Question
Related Questions
How do I keep my character facing the direction of travel after movement stops? 1 Answer
Script works in Game View, but not in build 0 Answers
Can I know why I can't control volume of audiomixergroup without exposing parameters? 0 Answers
Rotating cube steadily around world axis not working properly 0 Answers
cover more distance in the same time to keep gameObjects connected when rotating 0 Answers