Почему не работает, нанесение урона игроку?
Имеется здание (Игрок) и враг. Враг движется к зданию и наносит урон при касании. Подскажите, что не так с кодом?
Это здание:
public class Building : MonoBehaviour
{
public int health = 100;
public int level = 1;
private Rigidbody2D rb;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
rb.bodyType = RigidbodyType2D.Kinematic;
}
public void TakeDamage(int attackDamage)
{
health -= attackDamage;
if (health <= 0)
{
Destroy(gameObject);
}
}
public void Upgrade()
{
level++;
}
}
Это враг:
public class Enemy : MonoBehaviour
{
public float movementSpeed = 5f;
public int attackDamage = 10;
private Transform target;
private void Start()
{
target = GameObject.FindGameObjectWithTag("Building").transform;
}
private void Update()
{
// Движение врага к зданию
transform.position = Vector2.MoveTowards(transform.position, target.position, movementSpeed * Time.deltaTime);
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Building"))
{
// Атака здания
Building building = other.GetComponent<Building>();
if (building != null)
{
building.TakeDamage(attackDamage);
}
}
}
}