We are making a top down shooter in 2d and nothing we try to make the bullets do damage works.
This is our current code for the bullet it is currently not detecting when it hits an enemy:
public class Bullet : MonoBehaviour
{
public GameObject hitEffect;
private void OnCollisionEnter2D(Collision2D collision)
{
GameObject effect = Instantiate(hitEffect, transform.position, Quaternion.identity);
Destroy(effect, 2f);
Destroy(gameObject);
}
private void Update()
{
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.up, distance, whatIsSolid);
if (hitInfo.collider != null)
{
if (hitInfo.collider.CompareTag("Enemy"))
{
Debug.Log("Hit");
}
}
}
}