Advertisement
Pro_Unit

Projectile

May 8th, 2022
788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 KB | None | 0 0
  1.  public class Projectile : MonoBehaviour
  2.     {
  3.         [SerializeField] private Rigidbody _rigidbody;
  4.         [SerializeField] private float _speed = 100;
  5.         [SerializeField] private float _lifeTime = 10f;
  6.  
  7.         private Vector3 _direction;
  8.  
  9.         public Action<GameObject> TargetDetected;
  10.  
  11.         private void Awake() =>
  12.             Destroy(gameObject, _lifeTime); // Лучше использовать пул объектов
  13.  
  14.         public void Throw(Vector3 direction) =>
  15.             _direction = direction;
  16.  
  17.         public void Stop() =>
  18.             _direction = Vector3.zero;
  19.  
  20.         private void FixedUpdate() =>
  21.             _rigidbody.velocity = _direction * _speed;
  22.  
  23.         private void OnCollisionEnter(Collision collision)
  24.         {
  25.             TargetDetected?.Invoke(collision.gameObject);
  26.             Destroy(gameObject);
  27.         }
  28.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement