Hello,
For some reason, lasers shot by enemies do not register hits on my player ship. I have copied and adjusted the code from the EnemyBehavior script into my PlayerContorller Script, added the heatlh variable, and even made sure the player ship had rigidbody2D and Polygon Collider (with checked Trigger box). The code does not report any bugs or errors, but it does not log a hit nor destroys the player’s ship. What could be the reason?
public float health = 250;
void OnTriggerEnter2D(Collider2D collider)
{
Projectile shot = collider.gameObject.GetComponent<Projectile>();
if (shot)
{
Debug.Log("Hit by a projectile");
health -= shot.GetDamage();
shot.Hit();
if (health <= 0)
{
Destroy(gameObject);
}
}
}