Advertisement
johnnygoodguy2000

Bullet.cs

Jun 15th, 2024
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.04 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Unity.VisualScripting;
  4. using UnityEngine;
  5.  
  6. public class Bullet : MonoBehaviour
  7. {
  8.     private Camera camera;
  9.  
  10.     private void Awake()
  11.     {
  12.         camera = Camera.main;
  13.     }
  14.  
  15.     private void Update()
  16.     {
  17.         DestroyWhenOffScreen();
  18.     }
  19.  
  20.     private void OnTriggerEnter2D(Collider2D collision)
  21.     {
  22.         if (collision.GetComponent<EnemyMovement>())
  23.         {
  24.             HealthController healthController = collision.GetComponent<HealthController>();
  25.             if (healthController != null)
  26.             {
  27.                 healthController.TakeDamage(10);
  28.             }
  29.             Destroy(gameObject);
  30.         }
  31.     }
  32.  
  33.     private void DestroyWhenOffScreen()
  34.     {
  35.         Vector2 screenPosition = camera.WorldToScreenPoint(transform.position);
  36.  
  37.         if (screenPosition.y > camera.pixelHeight || screenPosition.y < 0 || screenPosition.x > camera.pixelWidth || screenPosition.x < 0)
  38.         {
  39.             Destroy(gameObject);
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement