Rotator.cs 746 B

12345678910111213141516171819202122232425262728293031
  1. using UnityEngine;
  2. namespace Player
  3. {
  4. [RequireComponent(typeof(Collider2D))]
  5. [RequireComponent(typeof(Rigidbody2D))]
  6. public class Rotator : MonoBehaviour
  7. {
  8. private Rigidbody2D _rigidbody;
  9. private int _direction = -1;
  10. [SerializeField] private Jumper _jumper;
  11. [SerializeField] private float _angularVelocity = 120;
  12. private void OnJump()
  13. {
  14. _rigidbody.angularVelocity = _direction * _angularVelocity;
  15. _direction = -_direction;
  16. }
  17. private void Awake()
  18. {
  19. _rigidbody = GetComponent<Rigidbody2D>();
  20. _jumper.OnJump += OnJump;
  21. }
  22. private void OnDestroy() => _jumper.OnJump -= OnJump;
  23. }
  24. }