Trying to rotate an object (Cube) smoothly in 90 degrees by mouse/swipe directi

Hello! I need a little bit of help. Trying to rotate an object (cube) by 90 degrees, in every direction, by the direction of the mouse. I used euler angles before, but that wasn’t really viable. Would’ve needed way to much unnecessary code, to achieve something relatively simple. now, by using quaternions it works way easier, however, I’m just not able to figure out why the 90 degree rotations aren’t accurate and they even get worse with every additional rotation. Found the code online by seaching for the issue.

public class MoveCube06 : MonoBehaviour
{
public float speed = 90.0f;
private Quaternion qTo = Quaternion.identity;
private bool dragging = false;
private bool lerping = false;
private Vector3 mouseVec;
private Vector3 target;
private Vector3 input;

void Update()
{
if (Input.GetMouseButtonDown(0))
{
dragging = true;
}

if (Input.GetMouseButton(0) && dragging && !lerping)
{
mouseVec = new Vector3(-Input.GetAxis(“Mouse X”), -Input.GetAxis(“Mouse Y”), 0);
input = GetLargestAxis(mouseVec).normalized;
Debug.Log("input: " + input);
target += input;
qTo = Quaternion.LookRotation(input) * this.transform.rotation;
lerping = true;
}

if (lerping)
{
this.transform.rotation = Quaternion.RotateTowards(this.transform.rotation, qTo, speed * Time.deltaTime);

if (this.transform.rotation == qTo)
{
lerping = false;
}

}

}
Vector3 GetLargestAxis(Vector3 vec)
{
Vector3 absVec = new Vector3(Mathf.Abs(vec.x), Mathf.Abs(vec.y), 0);
Vector3 result;
if (absVec.x > absVec.y)
{
if (vec.x > 0)
{
result = new Vector3(1, 0, 0);
}
else
{
result = new Vector3(-1, 0, 0);
}
}
else if (absVec.y > absVec.x)
{
if (vec.y > 0)
{
result = new Vector3(0, 1, 0);
}
else
{
result = new Vector3(0, -1, 0);
}
}
else
{
result = new Vector3(0, 0, 0);
}

return result;
}
}

Welcome to compounding rounding errors.
Computers dont have infinite precision.
If you work on a number in a loop thousands of times then the number will probably drift away.
As long as you dont write your code so that you rely on perfect precision then everything is good.

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

To achieve perfect rotation, adjust the desired rotation INSTANTLY by the desired amount (eg +90 or -90).

Then use a tweening mechanism to smoothly change current rotation to the desired rotation. Here’s a simple linear mechanism:

Smoothing movement between any two particular values:

You have currentQuantity and desiredQuantity.

  • only set desiredQuantity
  • the code always moves currentQuantity towards desiredQuantity
  • read currentQuantity for the smoothed value

Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

The code: SmoothMovement.cs · GitHub

In all cases, NEVER read values back from the .eulerAngles. Google “gimbal lock” for why.

Thanks for the fast reply. I’ll look into your suggestions and provided material. Sorry about the tags, this was my first time posting. I don’t do this very often.

1 Like