Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using Unity.VisualScripting;
- using UnityEngine;
- public class Bullet : MonoBehaviour
- {
- private Camera camera;
- private void Awake()
- {
- camera = Camera.main;
- }
- private void Update()
- {
- DestroyWhenOffScreen();
- }
- private void OnTriggerEnter2D(Collider2D collision)
- {
- if (collision.GetComponent<EnemyMovement>())
- {
- HealthController healthController = collision.GetComponent<HealthController>();
- if (healthController != null)
- {
- healthController.TakeDamage(10);
- }
- Destroy(gameObject);
- }
- }
- private void DestroyWhenOffScreen()
- {
- Vector2 screenPosition = camera.WorldToScreenPoint(transform.position);
- if (screenPosition.y > camera.pixelHeight || screenPosition.y < 0 || screenPosition.x > camera.pixelWidth || screenPosition.x < 0)
- {
- Destroy(gameObject);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement