I cant get raycasting to work in my unity 2d

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

public class Turret : MonoBehaviour
{
[SerializeField] float range;

public Transform target;

bool detected = false;

public Vector2 direction;

public GameObject Top;

void Update()
{   
    DetectPlayer();
}

void DetectPlayer()
{
    Vector2 targetPos = target.position;

    direction = targetPos - (Vector2)transform.position;

    RaycastHit2D rayinfo = Physics2D.Raycast(transform.position, direction, range);

    if (rayinfo)
    {
        if (rayinfo.collider.gameObject.tag == "Player")
        {   
            if (detected == false)
            {
              detected = true;  
            }
        }

        else
        {
            if (detected == true)
            {
              detected = false;  
            }
        }     
    }

    if (detected)
    {
        Top.transform.up = direction;
    }
}

void OnDrawGizmosSelected()
{
    Gizmos.DrawWireSphere(transform.position, range);
}

}

This is the code.

I have put the right things into the public variables. The problem I have is that my turret is moving but it is not tracking the player. It’s almost like the top is trying to face away from the player. Any help would be welcome!

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

public class Turret : MonoBehaviour
{
[SerializeField] float range;

public Transform target;

bool detected = false;

public Vector2 direction;

public GameObject Top;
public GameObject projectile;
public Transform shootPoint;

[SerializeField] float fireRate;

float nextTimeToFire = 0;

[SerializeField] float force;

void Update()
{   
    DetectPlayer();
}

void DetectPlayer()
{
    Vector2 targetPos = target.position;

    direction = targetPos - (Vector2)transform.position;

    RaycastHit2D rayinfo = Physics2D.Raycast(transform.position, direction, range);

    if (rayinfo)
    {
        if (rayinfo.collider.gameObject.tag == "Player")
        {   
            if (detected == false)
            {
              detected = true;  
            }
        }

        else
        {
            if (detected == true)
            {
              detected = false;  
            }
        }     
    }

    if (detected)
    {
        Top.transform.up = direction;
        if (Time.time > nextTimeToFire)
        {
            nextTimeToFire = Time.time+1/fireRate;
            Shoot();
        }
    }

}

void Shoot()
{
    GameObject bulletIns = Instantiate(projectile, shootPoint.position, Quaternion.identity);
    bulletIns.GetComponent<Rigidbody2D>().AddForce(direction * force);
}

void OnDrawGizmosSelected()
{
    Gizmos.DrawWireSphere(transform.position, range);
}

}

I can shoot. The tracking for the bullets work and they shoot at the player. It is just that the top part of the turret is not following them. I want to use ray casting btw so I can’t use the thing u suggested. Do you have any insight on this?
@UnityM0nk3y

I also tried your fix and it is still not moving right.

would sorting layers have anything to do with it?

I think it might be bc of this line “Top.transform.up = direction”