So i want to be able to rotate the cube in 2 directions when i press key up down or left right
the cube need to be able to rotate like the brown arrow (1 and 2) but never like the red one (3)
here is my code so far :
public float smooth = 1f;
private Quaternion targetRotation;
// Start is called before the first frame update
void Start()
{
targetRotation = transform.rotation;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("right"))
{
StopAllCoroutines();
targetRotation *= Quaternion.AngleAxis(90, Vector3.right);
StartCoroutine(RotateX(targetRotation));
}
if (Input.GetKeyDown("left"))
{
StopAllCoroutines();
targetRotation *= Quaternion.AngleAxis(90, Vector3.left);
StartCoroutine(RotateX(targetRotation));
}
if (Input.GetKeyDown("up"))
{
StopAllCoroutines();
targetRotation *= Quaternion.AngleAxis(90, Vector3.up);
StartCoroutine(RotateX(targetRotation));
}
if (Input.GetKeyDown("down"))
{
StopAllCoroutines();
targetRotation *= Quaternion.AngleAxis(90, Vector3.down);
StartCoroutine(RotateX(targetRotation));
}
IEnumerator RotateX(Quaternion targetRotation)
{
while (transform.rotation != targetRotation)
{
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, 10 * smooth * Time.deltaTime);
yield return null;
}
}
}
this code works well but the cubes rotates in all direction and i dont know how to "lock itâ to only rotate on 2 axis.
sorry for my english, it-s not my mothertongue.
thank you in advance for your help and have a good day everyone