Hi,
I have the following snippet of code, and amongst other things, it rotates a cube using Euler angles (180 degrees in the chosen direction), visually it seems to work.
Now, if I place a visual test on the cube, for instance, a smaller cube without a box collider on top, it still works, flips 180, but then the cube is always upside down, meaning it never fully flips back up (180), even though it goes through the rotation animation again, I’m just testing ‘right’ movement for now, but all other directions do the same thing ?
Any ideas or suggestions on how to rotate ‘back’ again, if I’m ‘already’ rotated ? Thanks.
IEnumerator CubeMovement(MoveDirection moveDirection, float duration)
{
canMove = false;
float progress = 0f;
Quaternion endRotation = Quaternion.Euler(0, 0, 0);
Vector3 targetPosition = new Vector3(currentCubePosition.x + Distance, currentCubePosition.y, currentCubePosition.z);
if (moveDirection == MoveDirection.UP)
{
endRotation = Quaternion.Euler(-180, 0, 0);
targetPosition = new Vector3(currentCubePosition.x, currentCubePosition.y, currentCubePosition.z + Distance);
}
else if (moveDirection == MoveDirection.DOWN)
{
endRotation = Quaternion.Euler(180, 0, 0);
targetPosition = new Vector3(currentCubePosition.x, currentCubePosition.y, currentCubePosition.z - Distance);
}
else if (moveDirection == MoveDirection.LEFT)
{
endRotation = Quaternion.Euler(0, 0, -180);
targetPosition = new Vector3(currentCubePosition.x - Distance, currentCubePosition.y, currentCubePosition.z);
}
else if (moveDirection == MoveDirection.RIGHT)
{
endRotation = Quaternion.Euler(0, 0, 180);
targetPosition = new Vector3(currentCubePosition.x + Distance, currentCubePosition.y, currentCubePosition.z);
}
Vector3 endPosition = new Vector3(targetPosition.x, currentCubePosition.y, targetPosition.z);
while (progress < duration)
{
progress += Time.deltaTime;
float percent = Mathf.Clamp01(progress / duration);
float height = jumpHeight * Mathf.Sin(Mathf.PI * percent);
transform.position = Vector3.Lerp(currentCubePosition, endPosition, percent) + new Vector3(0, height, 0);
transform.rotation = Quaternion.Lerp(currentCubeRotation, endRotation, percent);
yield return null;
}
currentCubePosition = new Vector3(transform.position.x, 0.5f, transform.position.z);
canMove = true;
}
}