Raycast won't change bool to true.

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class PetAi : MonoBehaviour
{
public bool attacking;
public float meleepetattackrange = 3;

public void Attack()
{      
    if(!attacking)
    {
        RaycastHit hit;

        if (Physics.Raycast(transform.forward, transform.TransformDirection(Vector3.forward), out hit, meleepetattackrange))
        {
           if(hit.collider.tag == "Dummy")
            {
                attacking = true;
            }
        }
    }
}

void OnCollisionStay(Collision collision)
{
    {
        if(attacking)
        {
            EnemyHealth enemyHealth = collision.gameObject.GetComponent<EnemyHealth>();
            if (enemyHealth != null)
            {
                enemyHealth.TakeDamage(petdamage);
                Debug.Log("petdamage");
                attacking = false;
            }
        }       
    }
}

}

I’m try to cast a ray when attacking = false so the enemy we’ll check for distant before attacking again but the ray won’t change the bool.

The first vector passed in to Physics.RayCast should be the origin, not a direction. So transform.direction doesn’t make sense- I would assume you’d want to use transform.position.