- Home /
iTween - Rotation Add get's uneven after several rotations
I hope it's alright if I post an iTween question here... Either way, here it is.
My current setup is a camera on my screen, that is set to rotate 90 degrees when you hit the right or left arrows. (it rotates 90 degrees left, with the left arrow, 90 degrees right with the right arrow.)
Here lies my problem. If I rotate it to the left say, it will be fine. Same for the right. But if I keep rotating it different directions, eventually, instead of being a solid number (remember, all I'm doing is adding 90 degrees left and right) it's something like 180.4532 degrees, or 90.346. If I keep doing this, it get's worse. All I'm doing is using RotationAdd to add 90 to the Z of my camera when I press a key. Why would it be doing this?
-Thanks
Answer by robertbu · Apr 03, 2014 at 04:53 AM
Anytime you have a relative rotations, there will be drift. Part of the drift is related to the the imprecision of floating point numbers. In addition, it is easy when coding relative rotations to under or overshoot a bit on the last frame. Regardless of why it is happening, the solution is to use an absolute rotation. If you are only rotating by 90 on one axis, then keep a integer count of the number of 90 degree turns. Add or subtract one from the integer count and then use the result in an absolute rotation. For example iTween.RotateTo(). Or you could keep your own float value and round it to 90 each time you increment/decrement it.
I can picture this in my head, but I'm having a hard time understanding how I would code this.. An example would be fantastic.
Here is a bit of example code. One of the problems with using iTween is that you have to deal with the potential of multiple iTweens fighting. I handle this in the example code by use an 'oncomplete' hash value and forcing the user to wait until the current rotation is complete before allowing a new one to start:
#pragma strict
var rotCount = 0;
var rotating = false;
function Update() {
if (!rotating && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftArrow)) {
rotCount = (rotCount + 1) % 4;
DoRotation();
}
if (!rotating && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.RightArrow)) {
rotCount = (rotCount - 1) % 4;
DoRotation();
}
}
function DoRotation() {
rotating = true;
var rot = rotCount * 90.0;
iTween.RotateTo(gameObject, {"z":rot, "oncomplete":"EndRotation", "time":1.25});
}
function EndRotation() {
rotating = false;
}
Although I'm using C# I completely understand what you are saying. Let me see what I can do.
Here is a C# translation:
using UnityEngine;
using System.Collections;
public class Example : $$anonymous$$onoBehaviour {
int rotCount = 0;
bool rotating = false;
void Update() {
if (!rotating && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftArrow)) {
rotCount = (rotCount + 1) % 4;
DoRotation();
}
if (!rotating && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.RightArrow)) {
rotCount = (rotCount - 1) % 4;
DoRotation();
}
}
void DoRotation() {
rotating = true;
var rot = rotCount * 90.0;
iTween.RotateTo(gameObject, iTween.Hash("z",rot, "oncomplete","EndRotation", "time",1.25));
}
void EndRotation() {
rotating = false;
}
}
Answer by JoeysLucky22 · Dec 14, 2014 at 08:51 AM
Thanks for the snippet above! I wanted to rotate the object regardless if it was currently rotating or not so I just did this:
public class Example : MonoBehaviour {
private int rotCount = 0;
private bool rotating = false;
void Update() {
if (Input.GetKeyDown(KeyCode.LeftArrow)) {
rotCount = (rotCount + 1) % 4;
DoRotation();
}
if (Input.GetKeyDown(KeyCode.RightArrow)) {
rotCount = (rotCount - 1) % 4;
DoRotation();
}
}
void DoRotation() {
float rot = rotCount * 90.0f;
if(rotating){
//If the object is currently rotating, instantly rotate it to its destination
iTween.RotateTo(gameObject, iTween.Hash("z",rot, "oncomplete","EndRotation", "time",0));
}
rotating = true;
iTween.RotateTo(gameObject, iTween.Hash("z",rot, "oncomplete","EndRotation", "time",1.25));
}
void EndRotation() {
rotating = false;
}
}
Your answer
Follow this Question
Related Questions
Autorotation issue when exiting gamecenter 0 Answers
iTween Path 2D rotation, orientToPath issue. 1 Answer
Can't lock rotation of object. 0 Answers
LookAt inaccurate rotation issue (javascript) 1 Answer
iTween Rotation not exact 0 Answers