I'm making a game where I'd like the player to be able to rotate a cube by 90 degrees on the x or z axis by pressing a button. I've gotten fairly far along in the process, but I've run into two problems here. #1. When using the following code, the x-rotation will sometimes get stuck toggling between 90 and -180 degrees when scrolling forward. #2. I'd like to be able to rotate the cube always to the players perspective (for example when the player right clicks the cube will always rotate 90 degrees to their right, instead of wherever the z-axis has been repositioned by the other rotation). Any help with either of these issues would be greatly appreciated.
private void Update()
{
if(Input.mouseScrollDelta.y > 0 && !isRotating)
{
StartCoroutine(Roll90(new Vector3(90, 0, 0)));
}
if (Input.mouseScrollDelta.y < 0 && !isRotating)
{
StartCoroutine(Roll90(new Vector3(-90, 0, 0)));
}
if(Input.GetMouseButtonDown(0) && !isRotating)
{
StartCoroutine(Roll90(new Vector3(0, 0, 90)));
}
if (Input.GetMouseButtonDown(1) && !isRotating)
{
StartCoroutine(Roll90(new Vector3(0, 0, -90)));
}
}
IEnumerator Roll90(Vector3 rot)
{
currentRotation = transform.eulerAngles;
targetRotation = currentRotation + rot;
isRotating = true;
if(rot.x != 0)
{
while (currentRotation.x != targetRotation.x)
{
currentRotation.x = Mathf.MoveTowardsAngle(currentRotation.x, targetRotation.x, rotationTime * Time.deltaTime * 100);
transform.eulerAngles = currentRotation;
yield return null;
}
if(currentRotation.x != targetRotation.x)
{
transform.eulerAngles = targetRotation;
}
}
if (rot.z != 0)
{
while (currentRotation.z != targetRotation.z)
{
currentRotation.z = Mathf.MoveTowardsAngle(currentRotation.z, targetRotation.z, rotationTime * Time.deltaTime * 100);
transform.eulerAngles = currentRotation;
yield return null;
}
if (currentRotation.z != targetRotation.z)
{
transform.eulerAngles = targetRotation;
}
}
isRotating = false;
}
```