- Home /
Rotation seems to pass through condition checks
Hello.
I have a slight problem (well, kinda big) when it comes to rotating my Camera. It will not stop rotating and continues to rotate even after turning a full 360 along the X axis
The only thing I wish to accomplish is rotating the Camera by 90 degrees so it looks towards the ground.
IEnumerator Rotate_GodMode_Camera()
{
// This function will explicitly deal with the rotation of the Camera.
while (_godMode)
{
if (Camera.main.transform.rotation.x != 90f)
{
Camera.main.transform.Rotate(new Vector3(90 * Time.deltaTime, 0, 0)); //Rotate the Camera by 90 degrees every second
yield return new WaitForSeconds(.001f);
}
else if (Camera.main.transform.rotation.x >= 90f)
{
break;
}
}
}
So when the input is detected, it starts this Coroutine.
I plan on adding a lot more to this once this basic part is figured out, and a coroutine is the way that I need to go to accomplish what I want.
I managed to get this to work if I use a generic i int variable and ++ after 1 second of WaitForSeconds, but is there a reason why this seems to be ignoring conditions?
Answer by Eric5h5 · Feb 18, 2013 at 07:48 PM
Transform.rotation is a 4D quaternion, which does not use degrees (everything is normalized), nor do the x/y/z elements directly correspond to x/y/z axes.
$$anonymous$$an I wasn't even looking at rotation, I just saw != and was like "Nope, floats can't not != something". :S
You can use rotation.eulerAngles.x ins$$anonymous$$d of rotation.x to get your rotation values. See the page on Quaternions (rotation is a Quaternion; don't try to understand what they are unless you Really Love $$anonymous$$ath A Lot, just how to use them)
Although reading individual elements of eulerAngles often fails, since there's more than one valid way to convert quaternions to eulerAngles (e.g., <0, 0, 0> is the same as <180, 180, 180>). It's better to lerp the rotation from one to another ins$$anonymous$$d of trying to check conditions. Also don't do things like "yield return new WaitForSeconds(.001f);", just yield a frame ins$$anonymous$$d. See the Rotation function here.
Okay, thanks for the information guys!
I am still in the process of figuring out exactly what to do (as this is a lot of information) so I'll post anything else in about an hour (probably year) when I kind-of understand what to do lol
Gonna start playing around with Eulers and Lerps though. **
Figured it out. (at the moment) my final code is:
IEnumerator Rotate_God$$anonymous$$ode_Camera()
{
/*
* This function will explicitly deal with the rotation of the Camera.
*/
while (_god$$anonymous$$ode)
{
if (Camera.main.transform.rotation.x < 80f)//Need to fix this; will check the Quaternions of both objects.
{
Camera.main.transform.rotation = Quaternion.Lerp(Camera.main.transform.rotation, _rtsCameraPoint.transform.rotation, Time.deltaTime);
yield return new WaitForSeconds(Time.deltaTime);
}
else if (Camera.main.transform.rotation.x >= 90f) //Need to fix this as well, but again, won't be a problem
{
break;
}
}
}
So what I ended up doing was basing my rotation problem around a Lerp which rotates it over a period of time. I got the initial rotation from the Camera itself, and I got the projected rotation (to rotation) from the empty object I created called _rtsCameraPoint.
What I will do in the future is create a variable to hold the Quaternion value of the rotation of the empty object, but for now everything is working very well!
(I know that the code has some serious flaws, namely the conditions. Gonna fix that up now but thought I'd drop my answer here before I did so)
Thanks guys!