47 lines
1.2 KiB
C#
47 lines
1.2 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
|
|
public class CubeRotator : MonoBehaviour
|
|
{
|
|
// Speed of rotation
|
|
public float rotationSpeed = 50f;
|
|
|
|
// Cubes to rotate
|
|
public GameObject[] cubes;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
// Assign random rotation axis to each cube
|
|
foreach (GameObject cube in cubes)
|
|
{
|
|
Vector3 randomAxis = Random.insideUnitSphere;
|
|
RotateCube(cube, randomAxis, rotationSpeed);
|
|
}
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
// Cubes rotate automatically, no need to do anything in Update
|
|
}
|
|
|
|
// Rotate cube around its axis
|
|
void RotateCube(GameObject cube, Vector3 axis, float speed)
|
|
{
|
|
// Start rotation coroutine for the cube
|
|
StartCoroutine(RotateCoroutine(cube.transform, axis, speed));
|
|
}
|
|
|
|
// Coroutine for rotating the cube
|
|
IEnumerator RotateCoroutine(Transform cubeTransform, Vector3 axis, float speed)
|
|
{
|
|
while (true)
|
|
{
|
|
// Rotate the cube around its axis
|
|
cubeTransform.Rotate(axis, speed * Time.deltaTime);
|
|
yield return null;
|
|
}
|
|
}
|
|
}
|