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;
}
}