Make enemy to "see" player in Unity 2d

Hello,

I’m making top down game (Hotline Miami style).

My problem is: how to make raycast or linecast (to detect player) form enemy eyes. Enemy can turn to face player after he detect him, so raycast from must rotate with enemy to emulate enemy vision direction.

To explain more:
My problem is: how to draw a raycast in front of the enemy (just like from enemy’s eyes), and rotate it when enemy rotates.

Checking if a character see something is not a problem.

transform.forward will give you the direction the object is facing

 RaycastHit2D hit = Physics2D.Raycast((Vector2) transform.position, (Vector2) transform.forward, distance);

3rd argument is distance ( length)

Ok, i upgrade the solution with ‘fov’ and simple vision cone form my enemies.

distanceToPlayer = Vector3.Distance ((Vector2)transform.position, (Vector2)playerPosition.position);
		
angle = Vector2.Angle (transform.up, playerPosition.position - transform.position);
	
		if (distanceToPlayer < rng)  //range = known float
		{
			playerInRange = true;
			if(angle < fov*0.5f) //fov = know float
			{

			// do something
	
			}

It’s worked form me well.

Thanks everybody for answer.

Contrary to what ryandotdee says, in top-down perspective it’s transform.up that gives you the direction your character is pointing (provided that the original imported sprite is facing upwards)

Similarly, a sprite which faces downwards in the sprite sheet would use transform.down