Pro_Unit

Bullet

Jan 7th, 2021 (edited)
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.02 KB | None | 0 0
  1. using UniRx;
  2. using UnityEngine;
  3.  
  4. public class Bullet : MonoBehaviour
  5. {
  6.     [SerializeField] private float _speed = 10f;
  7.  
  8.     private Damager _damager;
  9.  
  10.     private Vector2 _direction;
  11.     private Rigidbody2D _rigidbody;
  12.  
  13.     private void Awake()
  14.     {
  15.         _rigidbody = GetComponent<Rigidbody2D>();
  16.         _damager = GetComponent<Damager>();
  17.         name = $"Bullet_{GetInstanceID()}";
  18.     }
  19.  
  20.     public void Setup(Vector2 direction, GameObject owner, int damage, Subject<GameObject> OnKill)
  21.     {
  22.         _direction = new Vector2(direction.x, 0);
  23.  
  24.         transform.rotation = Quaternion.Euler(0, 0, direction.x > 0 ? -90 : 90);
  25.  
  26.         _damager.Owner = owner;
  27.         _damager.Damage = damage;
  28.  
  29.         _damager
  30.             .OnFinalDamage
  31.             .TakeUntilDisable(this)
  32.             .Subscribe(health =>
  33.             {
  34.                 OnKill.OnNext(health.Owner);
  35.             });
  36.     }
  37.  
  38.  
  39.     private void Update()
  40.     {
  41.         _rigidbody.velocity = _direction * (_speed * Time.deltaTime);
  42.     }
  43. }
Add Comment
Please, Sign In to add comment