Raycast

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Scrypt : MonoBehaviour

{

    public Transform target;

    public float range;

    Vector2 direction;

    Vector2 targetPos; 

    void FixedUpdate()

    {

        targetPos = target.position;

        direction = targetPos - (Vector2)transform.position;

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


        if (rayInfo)

        {

            if (rayInfo.collider.gameObject.tag == "Player")

            {

                transform.position = Vector3.Lerp(transform.position, target.position, 0.03f);

            }

        }

    }


    void OnDrawGizmosSelected()

    {

        Gizmos.DrawWireSphere(transform.position, range);

    }

}