I’m working on a game where a player needs to rotate a cube by 90 degrees on various axes by pressing buttons. The cube always needs to rotate relative to the player, meaning that if the player presses left, the cube will always rotate to the left. I thought I could solve this by having the cube be the child of another object, then using Quaternion AngleAxis with the axis specified as the parent’s right or up, that would solve it. However, it’s only giving me seemingly random rotations on many axis. Any help would be greatly appreciated.
public class CubeRoll : MonoBehaviour
{
public float rotationTime;
public bool isRotating;
Quaternion currentRotation, targetRotation;
private void Update()
{
if (Input.mouseScrollDelta.y > 0 && !isRotating)
{
StartCoroutine(Roll90(transform.parent.right, 1));
}
if (Input.mouseScrollDelta.y < 0 && !isRotating)
{
StartCoroutine(Roll90(transform.parent.right, -1));
}
if (Input.GetMouseButtonDown(0) && !isRotating)
{
StartCoroutine(Roll90(transform.up, 1));
}
if (Input.GetMouseButtonDown(1) && !isRotating)
{
StartCoroutine(Roll90(transform.up, -1));
}
Debug.DrawRay(transform.position, transform.parent.right);
Debug.DrawRay(transform.position, transform.parent.up);
Debug.DrawRay(transform.position, transform.parent.forward);
}
IEnumerator Roll90(Vector3 rAxis, int direction)
{
currentRotation = transform.rotation;
transform.rotation = Quaternion.AngleAxis(90 * direction, rAxis);
yield return null;
}
}