Bullet.cs 451 B

123456789101112131415161718192021
  1. using UnityEngine;
  2. public class Bullet : MonoBehaviour
  3. {
  4. public float speed = 20f;
  5. void Update()
  6. {
  7. transform.Translate(Vector3.forward * speed * Time.deltaTime);
  8. Destroy(gameObject, 3f);// Destroy bullet after 3s
  9. }
  10. void OnTriggerEnter(Collider other)
  11. {
  12. if (other.CompareTag("Target"))
  13. {
  14. other.GetComponent<Target>().TakeDamage();
  15. Destroy(gameObject);
  16. }
  17. }
  18. }