Hey guys. Ok, i’m at a loss on this one. I am trying to do something that should be simple. That is, have a box continuously tilt from right to left. The problem here is that I am getting inconsistent results from the ‘eulerangles’ which is causing weirdness.

Basically, when I run the script the box will tilt RIGHT then LEFT then RIGHT again and it never breaks out of the RIGHT state and just flips over. Any idea where I’m going wrong?

```
var i = 1000;
var trackRotation = 0;
var direction = "Right";
function FixedUpdate()
{
if (direction == "Left")
{
tiltLeft();
}
if (direction == "Right")
{
tiltRight();
}
if (direction == "StraightFromRight")
{
tiltStraightFromRight();
}
if (direction == "StraightFromLeft")
{
tiltStraightFromLeft();
}
}
function tiltRight()
{
trackRotation = transform.localEulerAngles.x;
if (trackRotation != 15 )
{
transform.Rotate(Vector3.right * Time.deltaTime*(i/30));
i--;
}
else if (trackRotation == 15)
{
direction = "StraightFromRight";
}
}
function tiltLeft()
{
trackRotation = transform.localEulerAngles.x;
if (trackRotation != 345 )
{
transform.Rotate(Vector3.left * Time.deltaTime*(i/30));
i--;
}
else if (trackRotation == 345)
{
direction = "StraightFromLeft";
}
}
function tiltStraightFromRight()
{
trackRotation = transform.localEulerAngles.x;
// Tilt straight from RIGHT
if (trackRotation <= 15 )
{
transform.Rotate(Vector3.left * Time.deltaTime*(i/30));
i++;
if (trackRotation == 0)
{
direction = "Left";
}
}
}
function tiltStraightFromLeft()
{
trackRotation = transform.localEulerAngles.x;
// Tilt straight from LEFT
if (trackRotation >= 345 || trackRotation == 0 )
{
transform.Rotate(Vector3.right * Time.deltaTime*(i/30));
i++;
if (trackRotation == 0)
{
direction = "Right";
}
}
}
```

[/quote]

I can see two things that can go wrong in your script

```
trackRotation = transform.localEulerAngles.x;
if (trackRotation != 15 )
{
transform.Rotate(Vector3.right * Time.deltaTime*(i/30));
i--;
}
else if (trackRotation == 15)
{
direction = "StraightFromRight";
}
```

First a general note/advise. localEulerAngles.x is a float, but as you haven’t declared the type of the trackRotation variable, Unity is treating it as an int in the comparison in your **if** (which in this case is actually lucky, as it would never equal 15 if treated as a float. It is a much better coding practice to always use >= or <= in these cases)

This leads strait into the first possible problem. If you rotate fast enough or has a low enough framerate, the eulerAngle could easily go from 14 to 16 in one update, n which case your script wont notice that you’ve reached the target tilt.

The second thing is that you can’t rely on a single value from the eulerAngles to do this kind of check. Unity stores rotations a quaternions and just convert these into eulerAngles for human readability, but a quaternion has multiple euler representations. E.g. if I create a new object in Unity and enters the euler rotation (100, 0, 0) in the inspector and forces and update (e.g. by switching to simple and back to full) the euler angle changes to the equivalent euler angle (80, 180, 180). Thus you can’t expect the localEulerAngles.x to be a linear value.

My advise is to maintain an internal representation of the object angle. E.g.

```
var tiltRight = true; //Which way are we rotation
var tiltSpeed : float = 30; //How fast are we rotation (deg/sec)
var maxAngle : float = 15; //The maximal right rotation
var minAngle : float = -15; //The maximal left rotation
var currentAngle : float = 0; //How much are we currently rotated
function Update() {
//How much would we like to rotate this frame
var deltaAngle = tiltSpeed * Time.deltaTime;
//How much can we rotate this frame
var remainingAngle = Mathf.Abs((tiltRight ? maxAngle : minAngle) - currentAngle);
//Ajust deltaAngle based on how much we can rotate and what direction
deltaAngle = Mathf.Min(deltaAngle, remainingAngle) * (tiltRight ? 1 : -1);
//Increment curentAngle and do the rotation
currentAngle += deltaAngle;
transform.Rotate(Vector3.right * deltaAngle);
//If we have reached one of the endpoints, then reverse the rotation direction
if (currentAngle >= maxAngle || currentAngle <= minAngle) {
tiltRight = !tiltRight;
}
}
```

Hope that helps a little.