Advertisement
leomovskii

Bullet

Oct 5th, 2024 (edited)
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.59 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Bullet : MonoBehaviour {
  6.  
  7.     public float maxDistance = 500f;
  8.     public int damage = 1;
  9.  
  10.     Vector3 startCoords;
  11.  
  12.     private void Start() {
  13.         startCoords = transform.position;
  14.     }
  15.  
  16.     private void Update() {
  17.         if (Vector3.Distance(transform.position, startCoords) >= maxDistance) {
  18.             Destroy(gameObject);
  19.         }
  20.     }
  21.  
  22.     private void OnCollisionEnter(Collision collision) {
  23.         if (collision.gameObject.TryGetComponent(out Health health)) {
  24.             health.Damage(damage);
  25.         }
  26.  
  27.         Destroy(gameObject);
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement