using UnityEngine; using UnityEngine.Assertions; [RequireComponent(typeof(Collider))] public class Projectile : MonoBehaviour { [SerializeField, Range(0f, 1f)] private float bounciness = 0.5f; private Collider projCol; [SerializeField, Range(0, 50)] private int damage = 10; [SerializeField] private int wallRebounces = 4; private int hitWalls = 0; public HealthComponent comingFrom; private void Awake() { projCol = GetComponent(); Assert.IsNotNull(projCol); PhysicMaterial pMat = new(); pMat.bounciness = this.bounciness; projCol.material = pMat; } private void OnCollisionEnter(Collision collision) { HealthComponent hitHealthComp = collision.gameObject.GetComponent(); if (hitHealthComp == comingFrom) return; if (hitHealthComp) { hitHealthComp.TakeDamage(damage); Destroy(gameObject); } else { if (++hitWalls == wallRebounces) { Destroy(gameObject); } } } }