- Home /
Rotating by 90 degrees eventually locks
Back again. Currently using a coroutine to rotate a level around a pivot point (It is the child of the pivot point, so I am rotating the point and that makes everything work well)
Two problems. After 7 clockwise rotations, Quaternion.Dot(level.rotation, toangle) never reaches 1.
and counterclockwise rotations just bounce back and forth between the first move and back to the original position.
Here is my coroutine:
private IEnumerator SpinLevel(Vector3 direction)
{
player.transform.SetParent(this.transform); // adds the player object to the level, so it will rotate with the level and not get tossed out
toAngle = Quaternion.AngleAxis(startDegree + rotationDegree, direction);
while (Quaternion.Dot(level.rotation, toAngle) < 1.0f)
{
transform.rotation = Quaternion.RotateTowards(transform.rotation, toAngle, rotateSpeed*Time.deltaTime);
yield return null;
}
yield return new WaitForEndOfFrame();
startAngle = transform.rotation;
levelRotating = false;
rotatingCounterClock = false;
rotatingClockwise = false;
player.transform.parent = null;
}
and here is the rest
[SerializeField] private float rotationDegree = 90f; // how far you want the level to rotate. May need to be made public in order to change it for different levels.
[SerializeField] private float rotateSpeed = 100f; // speed of the rotation
[SerializeField] GameObject player;
// public flags for use in other scripts. Other objects will need to know if the world is moving clock or counter clockwise, or even if it is moving
private bool rotatingClockwise = false, rotatingCounterClock = false, levelRotating = false;
private Quaternion toAngle, startAngle;
private float startDegree;
private Transform level;
private void Awake()
{
level = this.transform; //shorthand, chached.
startAngle = Quaternion.identity; // Make sure the level startAngle is set to No Rotation to start.
}
private void FixedUpdate()
{
if (!levelRotating)
{
startDegree = startAngle.eulerAngles.z;
if (Input.GetAxisRaw("Rotate") > 0)
{
//clockwise rotation via the e key
rotatingClockwise = true; // set the direction flag
levelRotating = true;
StartCoroutine(SpinLevel(-level.forward));
}
else if (Input.GetAxisRaw("Rotate") < 0)
{
//counterclockwise rotation via the q key
rotatingCounterClock = true;
levelRotating = true;
StartCoroutine(SpinLevel(level.forward));
}
} //else levelRotationg = true;
}
I had thought that using Quaternions was supposed to prevent the issue of getting locked... I do realize that Quaternion.Dot compares two floats and finds if they are 'close enough' - when I put a debug in the while loop, at the lock point Quaternion.dot just returns .99999 - (and I assume repeating) and never gts close enough for Dot to say they are equal.
I tried setting the transform after each rotation to hard set the eulerAngle - but that seems to defeat the purpose of using Quaternions anyways, and it didn't work.
I also tried using !MathF.Approximately(dot,1) rather than Dot < 1 but it still runs into the same issue (i assume because Dot uses something akin to Approximately)
and I have - no idea why the counterclockwise is bouncing between the two. In previous incarnations of this script, using vector3.forward and -vector3.forward was an easy way to move clock and counterclockwise without having to change the start/end angles. I don't know why it stopped working when I went to an While statement. (maybe it was only seeming to work in the first place)
Any help would be appreciated. I've been banging my head against this level rotation script all day.
Answer by troien · Feb 25, 2019 at 04:47 PM
As far as the "it is close to 1, but not exactly", did you try using the equals operator instead?
If you read the documentation you'll see that it returs true if 'close to' 1, and Unity probably implemented that differently then you did. (You can use != awell, but Unity hasen't got that one documented in the docs, so can't link to that)
while (level.rotation != toAngle)
As for the counter clockwise part. That is probably because of how you calculate the angle, Using 'startdegree' like that is not going to work in all cases, as when you start rotating, euler angles can flip halfway through a rotation because there can be more ways to display the same rotation in eulerangles, Unity is going to pick the one that is it's quaternion to euler conversion function has implemented, which might not be the one you expect.
Therefore, as input for your new rotation, don't just use the z axis of the rotation, use the entire rotation, like so:
toAngle = transform.rotation * Quaternion.AngleAxis(rotationDegree, direction);
.
Thank you!
I did try the == (and !=) issues, but recieved the exact same problem.
And I had figured out the second problem, but not a solution to it. That makes muchmore sense!
I have sense moved the code out of CoRoutine and into FixedUpdate with the rest of the control code, now that I have some sort of while fix. Still running into some issues, but getting much cleaner now.
I'll submit this as an answer to close this thread, because the problem has now changed, and what you have stated will work well for people googling it, I hope.
just FYI - oh my gooodnes. I can't believe it. just forgetting that tranform.rotation*quat aspect was what was causing 90% of my bugs. That has fixed most of my problems, even in my new code out of a coroutine. Thank you!
Answer by Lynkfox · Feb 25, 2019 at 08:02 PM
For anyone googling that comes across this in an attempt to make their own version, here is the final code I used: I did indeed switch to !=Angle, but instead of using RotateTowards or transform.rotate, I switched to Quaternion.Lerp. This removed the jerkyness when correcting the final position, and also seemed to prevent the floating point error with Quaternion.Dot
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateController : MonoBehaviour
{
[SerializeField] private float rotationDegree = 90f; // how far you want the level to rotate. May need to be made public in order to change it for different levels.
[SerializeField] private float rotateSpeed = 5f; // speed of the rotation
[SerializeField] GameObject player;
private Quaternion toAngle;
private Transform level;
private int rotateCount = 0;
private void Awake()
{
level = this.transform;
startAngle = Quaternion.identity;
}
private void FixedUpdate()
{
if (!levelRotating)
{
if (Input.GetAxisRaw("Rotate") > 0)
{
levelRotating = true;
rotateCount--;
toAngle = transform.rotation * Quaternion.AngleAxis(rotationDegree, -transform.forward);
}
else if (Input.GetAxisRaw("Rotate") < 0)
{
levelRotating = true;
rotateCount++;
toAngle = transform.rotation * Quaternion.AngleAxis(rotationDegree, transform.forward);
}
}
else // levelRotationg = true;
{
/* The actual rotate. This figures out Clockwise and Counterclockwise by switching the axis of the rotate. Forward rotates counter clockwise (Left Handed Cordinate System
* means that X -> Y.) -Forward will routate clockwise. This is done in the toAngle calculation above.
*/
if(transform.rotation != toAngle)
{
player.transform.SetParent(this.transform);
transform.rotation = Quaternion.Lerp(transform.rotation, toAngle, rotateSpeed / 100);
}
else //Cleanup
{
if( rotateCount < 360/rotationDegree)
{
transform.rotation = toAngle;
} else //destination rotate count is greater than or equal to the amount of times the degRotate can divide into a circle)
{
transform.rotation = Quaternion.identity;
rotateCount = 0;
/* Note:
*
* Currently no Error checking if rotationDegree is not a whole divisor of 360. Error checking will be needed in awake
*/
}
levelRotating = false;
player.transform.parent = null;
}
}
}
}
Your answer
Follow this Question
Related Questions
Quaternion.identity giving outlier result? 1 Answer
Store object's starting position and rotation and have it return there. 2 Answers
Rotate Gameobject by vertex shader with quaternion 0 Answers
Doesn't Add Rotation Over 180 2 Answers
rotate player on y axis or Vector.up regradless of orientation 0 Answers