using UnityEngine; using Code.Gameplay.Core; namespace Code.Gameplay.Weapons { public class Bullet : MonoBehaviour { [SerializeField] private float lifeTime = 3f; [SerializeField] private LayerMask hitLayers = -1; [SerializeField] private GameObject impactEffect; [SerializeField] private AudioClip impactSound; private float _speed; private float _damage; private Vector3 _direction; private bool _isInitialized; private AudioSource _audioSource; public void Initialize(float speed, float damage, Vector3 direction) { _speed = speed; _damage = damage; _direction = direction.normalized; _isInitialized = true; _audioSource = GetComponent(); Destroy(gameObject, lifeTime); } private void Update() { if (!_isInitialized) return; transform.position += _direction * (_speed * Time.deltaTime); transform.rotation = Quaternion.LookRotation(_direction); if (Physics.Raycast(transform.position, _direction, out var hit, _speed * Time.deltaTime, hitLayers)) { HitTarget(hit); } } private void HitTarget(RaycastHit hit) { var damageable = hit.collider.GetComponentInParent(); damageable?.TakeDamage(_damage); if (impactEffect != null) { Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal)); } if (impactSound != null && _audioSource != null) { _audioSource.PlayOneShot(impactSound); } Destroy(gameObject); } private void OnTriggerEnter(Collider other) { var damageable = other.GetComponentInParent(); damageable?.TakeDamage(_damage); if (impactEffect != null) { Instantiate(impactEffect, transform.position, Quaternion.identity); } Destroy(gameObject); } } }