For some reason the collision is not working. The game is a shooting-scroller, bullets pass through the enemies and the enemies pass through the player, and nothing is dealing damage. I just started to learn to code the other week so please help.
Bullet:
public int damage = 10;
// ...
void OnTriggerEnter(Collider other)
{
// Check if the bullet hits an enemy
if (other.CompareTag("Enemy"))
{
// Damage the enemy and destroy the bullet
other.GetComponent<EnemyController>().TakeDamage(damage);
Destroy(gameObject);
}
}
Enemy:
public int damage = 10;
public int health = 5;
// ...
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
// Damage the player when the enemy collides with them
other.GetComponent<PlayerController>().TakeDamage(damage);
Destroy(gameObject);
}
}
public void TakeDamage(int damage)
{
health -= damage;
if (health <= 0)
{
// Enemy defeated - drop coins and destroy the enemy
DropCoins();
Destroy(gameObject);
}
}
Player:
private int health = 1;
// ...
public void TakeDamage(int damage)
{
health -= damage;
if (health <= 0)
{
// Handle death logic
Debug.Log("Player died!");
}
}
In the collider on the gameobjects I have all marked with Is Trigger ☑
.