Help with moving object towards player using Raycasting

I made two scripts. 1 for the player and 1 for the AI. The player is supposed to look at the AI and that would cause the AI to move towards the player. When I test it out, it doesn’t work.
Player Script
{

    Ray thugRay;
    RaycastHit rayHit;
    public int dist = 20;
    void Update()
    {
        thugRay = new Ray(transform.position, transform.forward * dist);
        Debug.DrawRay(transform.position, transform.forward * dist, Color.red);

        if (Physics.Raycast(transform.position, transform.forward, out rayHit, dist))
        {
            {
                if (rayHit.transform.gameObject.tag == ("It"))
                {
                    Debug.Log("He looked at you");
                    GameObject.Find("Control").GetComponent("Move").enabled = false;


                }
            }
        }
    }
}

AI Script
{

    public Transform player;
    public float walkingDistance = 10.0f;
    public float smoothTime = 10.0f;
    private Vector3 smoothVelocity = Vector3.zero;

    RaycastHit rayHit;

    private void Start()
    {
        
    }

    void HitByRay()

    {

        Debug.Log("Rip");
        
        transform.LookAt(player);
        float distance = Vector3.Distance(transform.position, player.position);
        if (distance < walkingDistance)
        {
            transform.position = Vector3.SmoothDamp(transform.position, player.position, ref smoothVelocity, smoothTime);
        }
       

        rayHit.transform.SendMessage("HitByRay");

    }
}

Hello there,

could you give us more information on what works and what doesn’t please? Which Debug logs get triggered? Does your Debug Ray point towards the right direction?

Also, you say that you want the AI to move towards the player when looked at. But if the ray hits, you seem to be disabling the move component. Unless you are disabling the move component on the player, in which case it’s fine.

And finally, when you do see the AI, nothing tells it to do anything. You probably want to have your scripts like that instead:

    Ray thugRay;
    RaycastHit rayHit;
    public int dist = 20;
    private bool iAmDead = false;
    void Update()
    {
        if (iAmDead == false)
        {
            thugRay = new Ray(transform.position, transform.forward * dist);
            Debug.DrawRay(transform.position, transform.forward * dist, Color.red);

            if (Physics.Raycast(transform.position, transform.forward, out rayHit, dist))
            {
                if (rayHit.transform.gameObject.tag == ("It"))
                {
                    Debug.Log("He looked at you");
                    GameObject.Find("Control").GetComponent<Move>().enabled = false;
                    rayHit.collider.gameObject.GetComponent<AI>().HitByRay();
                    iAmDead = true;
                }
            }
        }
    }

And now for the AI:

 public Transform player;
    public float walkingDistance = 10.0f;
    public float smoothTime = 10.0f;
    private Vector3 smoothVelocity = Vector3.zero;

    RaycastHit rayHit;

    private void Start()
    {

    }

    public void HitByRay()
    {

        Debug.Log("Rip");

        transform.LookAt(player);
        float distance = Vector3.Distance(transform.position, player.position);
        if (distance < walkingDistance)
        {
            transform.position = Vector3.SmoothDamp(transform.position, player.position, ref smoothVelocity, smoothTime);
        }


        //rayHit.transform.SendMessage("HitByRay");

    }

I hope that helps!

~LegendBacon