ConstantRotation.cs 661 B

123456789101112131415161718192021222324252627282930313233
  1. using UnityEngine;
  2. using System.Collections;
  3. public class ConstantRotation : MonoBehaviour
  4. {
  5. public float minRotationSpeed = 100;
  6. public float maxRotationSpeed = 400;
  7. public bool canBeNegative;
  8. public Transform rotationPoint;
  9. float _rotationSpeed;
  10. Transform _transform;
  11. static Vector3 _zAxis = new Vector3(0, 0, 1);
  12. void Start()
  13. {
  14. _transform = transform;
  15. }
  16. void OnEnable()
  17. {
  18. _rotationSpeed = Random.Range(minRotationSpeed, maxRotationSpeed);
  19. if (canBeNegative)
  20. {
  21. _rotationSpeed *= Random.value < 0.5f ? -1 : 1;
  22. }
  23. }
  24. void Update()
  25. {
  26. _transform.RotateAround(rotationPoint.position, _zAxis, _rotationSpeed * Time.deltaTime);
  27. }
  28. }