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.