Advertisement
Pro_Unit

Bullet

Sep 22nd, 2022
827
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.83 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class Bullet : MonoBehaviour
  4. {
  5.     [SerializeField] private float _percentDamage = 25;
  6.     [SerializeField] private float _lifeTime = 5f;
  7.     [SerializeField] private float _moveSpeed = 3f;
  8.     [SerializeField] private Rigidbody _rigidbody;
  9.  
  10.     private void Start()
  11.     {
  12.         Destroy(gameObject, _lifeTime);
  13.     }
  14.  
  15.     public void Setup(Vector3 direction)
  16.     {
  17.         _rigidbody.velocity = direction * _moveSpeed;
  18.     }
  19.  
  20.     private void OnTriggerEnter(Collider other)
  21.     {
  22.         if (!other.CompareTag("Player") && other.TryGetComponent(out IDamageable damageable))
  23.         {
  24.             damageable.GetDamage(_percentDamage);
  25.  
  26.             Stop();
  27.  
  28.             Destroy(gameObject);
  29.         }
  30.     }
  31.  
  32.     private void Stop()
  33.     {
  34.         _rigidbody.velocity = Vector3.zero;
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement