I wrote this following code for detect collision between player and enemy when distance is 0.2f.
I want the Ray collision to occur only when the distance between the player and the enemy is 0.2. That is, if this distance is less or more, the collision will not be done.
var hit = Physics2D.Raycast(transform.position, child.transform.position, 0.2f, 1 << LayerMask.NameToLayer("Player"));
Look at the docs, the 2nd argument is not a world position, it’s a direction.
If you want to detect contacts between two points then you use Physics2D.LineCast.
It’s not clear if you’re interested in the distance between potential contacts or not because a ray from the player position to an enemy position would give you the distance from the player center to a contact which is an odd arbitrary value. Why not just use the distance i.e. magnitude of the vector (enemy position - player position)?