- Home /
Why inconsistant calculation when round to X degrees?
Hello all,
I have a script on my game object in a game of unlocking the door with a locker. When you place the icons on the lock in the correct order the door is opened. When I touch the screen, the reel is turning down / up on the X axis. Each icon in the reel is position in front of the player at exactly 45 degrees. total of 8 icons hence 360/8 = 45.
I have a script that manipulates the rotation. When touch is ended, I want the reel to "fall" on the closest icon, meaning, if after rotation, my reel shows 10, the closest icon is at 0, if it shows 80 the closest icon is at 90. I want the script to take the delta (d = 10) and add or subtract from the current rotation.
if (touch.phase == TouchPhase.Ended)
{
touchChangePosition = touchPosition - touch.position;
direction = touchChangePosition.y >= 0f ? 1 : -1;
if (angle % 45 != 0)
{
float rounded = Mathf.RoundToInt(myTransform.eulerAngles.x / 45) * 45; //understand wich number I need to reach that is closest to 45 multiplier
float currentRot = Mathf.RoundToInt(myTransform.eulerAngles.x);
angle = Mathf.Abs(currentRot - rounded);
print("the current rotation is: " + currentRot + " aim to: " + rounded + " angle is: " + angle);
if (currentRot > rounded)
{
myTransform.Rotate(Vector3.right, -1 * angle * direction); // take the angle value from the current rotation to the rounded number
}
if (currentRot < rounded)
{
myTransform.Rotate(Vector3.right, angle * direction);
}
print("Current rotation is: " + Mathf.RoundToInt(myTransform.eulerAngles.x) + " Direction: " + direction);
//print("EULER ANGLE AFTER" + myTransform.eulerAngles.x);
}
}
}
The script is pretty streight forward, if the current rotation number is bigger then my rounded number (the number that gives %45 = 0) then take the current rotation and subtract the delta, if the current rotation is smaller then add the delta to reach the rounded number.
but instead I get this: My expected result would be that when current rotation is 55 and rounded is 45, then the delta would be subtracted but it's the other way around... Can't tell what's causing this.
Any ideas?
Thanks.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
EularAngels Question / Lock a Rotation? 1 Answer
Mathf.Lerp won't stop snapping 2 Answers