Player loses health when they stand in a certain area

For some reason, when the player walks into a certain area of the level, they start to lose health.

I’ve checked to see if there is anything that could cause it such as empty objects with box colliders and I’ve stated in my code that if an enemy is not close enough to deal any damage then they won’t deal any at all.

I have no idea what I’m doing wrong so any help would be greatly appreciated :).

Edit

This is the code that controls the player.

public class PlayerController : MonoBehaviour
{
    Rigidbody2D rb;

    public float moveSpeed = 5f;
    public float maxHealth = 100f;
    public float currentHealth;
    public float minDamageDistance = 2f;
    public float angleOffset = 86f;

    public bool playerDead;

    public GameObject spider;

    public HealthBar healthBar;

    Vector2 movement;

    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
        currentHealth = maxHealth;
    }

    public void Start()
    {     
        healthBar.SetMaxHealth(maxHealth);
    }

    public void Update()
    {
        PlayerRotation();
        SpiderDamage();

        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");

        // Just for testing
        if (Input.GetKeyDown(KeyCode.Space))
        {
            currentHealth = maxHealth;
        }

        if (playerDead)
        {
            GetComponent<PlayerController>().enabled = false;
        }
    }

    private void FixedUpdate()
    {
        rb.MovePosition(rb.position + (movement * moveSpeed * Time.fixedDeltaTime));      
    }

    void SpiderDamage()
    {
        if (spider != null && Vector2.Distance(transform.position, spider.transform.position)
            <= minDamageDistance)
        {
            TakeDamage(5f);
        }
    }

    void PlayerRotation()
    {
        Vector2 positionOnScreen = Camera.main.WorldToViewportPoint(transform.position);
        Vector2 mouseOnScreen = Camera.main.ScreenToViewportPoint(Input.mousePosition);
        float angle = AngleBetweenTwoPoints(positionOnScreen, mouseOnScreen);
        transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, angle));
    }

    // Calculates the angle between the mouse position and the player
    float AngleBetweenTwoPoints(Vector3 a, Vector3 b)
    {
        return (Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg) + angleOffset;
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("NextSceneTrigger"))
        {
            FindObjectOfType<SceneManagment>().NextScene();
        }
        else if (other.CompareTag("PreviousSceneTrigger"))
        {
            FindObjectOfType<SceneManagment>().PreviousScene();
        }
    }

    public void TakeDamage(float damage)
    {
        currentHealth -= damage;

        healthBar.SetHealth(currentHealth);

        if(currentHealth <= 0 && !playerDead)
        {
            PlayerDeath();
            playerDead = true;
        }
    }

    void PlayerDeath()
    {
        FindObjectOfType<GameManager>().GameOver();
    }

}

Maybe you have a gameobject in your scene that you accidentally put an enemy tag on?

I see that you have nothing in your code assigning the spider to your player variable, so perhaps whatever spider you have assigned is not the one in the scene? Perhaps you added a prefab not in scene?

Debug. Log the object and check if it’s coordinates corresponds to movement of your spider.

Add debug.log in take damage function and track source

What I can see the code works as expected, your player is within 2 meters of distance of the spider linked to your script and the spider does damage to you. So the problem is not within the code, we need to look elsewhere to figure out what the issue is.

If this does not correspond to what you see on the screen there must be some other spider linked to your script than the spider you are looking at on the screen. Did you drag the spider from the project window to your script instead of the one in your scene? Did you drag a spider to your script while in game play mode and then when exiting it reverted to another spider than the one you thought you had linked? If you select the spider in the scene while playing does it’s coordinates match what you see in debug log?

Your code looks fine, I believe you are using the prefab of the spider instead of the one from the scene and that is causing the issue…