Raycast not hitting Capsule GameObject

I crated a First Person Controller, a big Cube GameObject to act as my ground and another Capsule GameObject to act as an enemy.
I’m trying to use Raycast to determine if my “melee” attack hit the enemy.

Here’s my code:

public class MeleeSystem : MonoBehaviour {
	public int damage = 50;
	public float distance;
	public float maxDistance = 5.5f;
	
	void Update()
	{
		if (Input.GetButtonDown("Fire1"))
		{
			RaycastHit hit = new RaycastHit();
			if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit))
			{
				distance = hit.distance;
				if (distance < maxDistance)
				{
					Debug.Log("HIT! - " + hit.distance);
					hit.transform.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
				}
			}
		}
	}
}

The Raycast hit the ground (big Cube GameObject) with no problem, but it seems like it just goes through the enemy (Capsule GameObject). When I try to actually walk “through” the enemy - I can’t. So it does exists and uses it’s collider attribute.

What am I doing wrong?

Found problem.

Changed

if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit))

to

Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))

Now it works great!