3DTD/Assets/Scripts/Tower/Projectile.cs

50 lines
1.2 KiB
C#
Raw Normal View History

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