Advertisement
Guest User

Solar Falcon's Bullet Class

a guest
Feb 3rd, 2025
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.45 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. namespace _Scripts
  4. {
  5.     public class ParticleBullet : MonoBehaviour
  6.     {
  7.         public float speed = 5f;
  8.         private Vector3 direction;
  9.         [SerializeField] Collider[] colliders;
  10.         [SerializeField] GameObject[] particleSystems;
  11.         [SerializeField] GameObject hitEffect = null;
  12.         Rigidbody rb;
  13.         private bool active;
  14.         public int bulletPower = 1;
  15.         private float lifeTimer;
  16.         [SerializeField] float lifeTime = 4f;
  17.  
  18.  
  19.         private void Awake()
  20.         {
  21.             rb = GetComponent<Rigidbody>();
  22.         }
  23.  
  24.         void OnEnable()
  25.         {
  26.             lifeTimer = lifeTimer;
  27.         }
  28.  
  29.         public void SetBullet(Vector3 direction, float speed)
  30.         {
  31.             this.direction = direction;
  32.             this.speed = speed;
  33.         }
  34.    
  35.         public void SetDirection(Vector3 dir)
  36.         {
  37.             direction = dir.normalized;
  38.         }
  39.  
  40.         public void SetSpeed(float value)
  41.         {
  42.             speed = value;
  43.         }
  44.  
  45.         void Update()
  46.         {
  47.             lifeTimer -= Time.deltaTime;
  48.             if (lifeTimer <= 0 && active)
  49.             {
  50.                 var newpos = Vector3.forward * 1000;
  51.                 transform.position = newpos;
  52.                 Activate(false);
  53.             }
  54.             transform.position += direction * (speed * Time.deltaTime);
  55.             RotateTowardsHeading();
  56.         }
  57.        
  58.         void RotateTowardsHeading()
  59.         {
  60.             if (direction != Vector3.zero)
  61.             {
  62.                 Quaternion targetRotation = Quaternion.LookRotation(direction, Vector3.up);
  63.                 transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 1000f);
  64.             }
  65.         }
  66.  
  67.  
  68.         /// <summary>
  69.         /// Activate/Inactivate Bullet
  70.         /// </summary>
  71.         public void Activate(bool isActive)
  72.         {
  73.             active = isActive;
  74.  
  75.             rb.detectCollisions = isActive;
  76.  
  77.             if (colliders != null && colliders.Length > 0)
  78.             {
  79.                 for (int i = 0; i < colliders.Length; i++)
  80.                 {
  81.                     colliders[i].enabled = active;
  82.                 }
  83.             }
  84.  
  85.             if (particleSystems != null && particleSystems.Length > 0)
  86.             {
  87.                 for (int i = 0; i < particleSystems.Length; i++)
  88.                 {
  89.                     particleSystems[i].SetActive(active);
  90.                 }
  91.             }
  92.  
  93.             if (!isActive)
  94.             {
  95.                 if (hitEffect != null)
  96.                 {
  97.                     GameObject obj = ObjectPool.instance.SpawnPrefab(hitEffect);
  98.                     obj.transform.position = transform.position;
  99.                     obj.transform.rotation = Quaternion.identity;
  100.                 }
  101.                 ObjectPool.instance.ReturnToPool(gameObject);
  102.             }
  103.         }
  104.         private void OnCollisionEnter(Collision collision)
  105.         {
  106.             Collider collider = collision.collider;
  107.             if (collider.CompareTag("Enemy")
  108.                 || collider.CompareTag("Player"))
  109.             {
  110.                 // Damage the enemy
  111.                 IDamageable damageable = collider as IDamageable;
  112.                 if (damageable != null)
  113.                 {
  114.                     damageable.OnBulletStrike(bulletPower);
  115.                 }
  116.                 Activate(false);
  117.             }
  118.         }
  119.     }
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement