Make a Raycast follow an object

I’m trying to make this solider follow the player when it is in site. But the gun raycast doesn’t follow the player. Is their a way to make it so that the raycast can follow the player?

` Vector3 forward = transform.TransformDirection(Vector3.forward) * rangeForSoilder;
Debug.DrawRay(transform.position, forward, Color.green);
RaycastHit hitInfo;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hitInfo, rangeForSoilder) && (Time.time > timeLastShot + delayBetweenShots))

    {
      
        var hitRotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
        timeLastShot = Time.time;
        Debug.Log(hitInfo.transform.name);
        hitornot = Random.Range(minRan, maxRan);

        if (hitInfo.transform.name == ("Player"))
        {

        }

        healthanddie HD = hitInfo.transform.GetComponent<healthanddie>();
        if (HD != null)
        {
            GunFire.Play();
            muzzleFlash.Play();

            if (hitornot == 1)
            {

            HD.TakeDamage(damage);
            var bloodvar = Instantiate(BloodTransform, hitInfo.point, hitRotation);
            bloodvar.transform.SetParent(hitInfo.transform, true);
                lookRadius = SeeRadiusSize;
            }
           
        }
    }`

Here a better form of the script

   Vector3 forward = transform.TransformDirection(Vector3.forward) * rangeForSoilder;
        Debug.DrawRay(transform.position, forward, Color.green);

        RaycastHit hitInfo;
        if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hitInfo, rangeForSoilder) && (Time.time > timeLastShot + delayBetweenShots))

        {
          

            var hitRotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);

            

            timeLastShot = Time.time;

            Debug.Log(hitInfo.transform.name);

            hitornot = Random.Range(minRan, maxRan);


            if (hitInfo.transform.name == ("Player"))
            {
            

                
            }

            healthanddie HD = hitInfo.transform.GetComponent<healthanddie>();
            if (HD != null)
            {
                

                GunFire.Play();
                muzzleFlash.Play();



                

          

                if (hitornot == 1)
                {

                
                HD.TakeDamage(damage);
                var bloodvar = Instantiate(BloodTransform, hitInfo.point, hitRotation);
                bloodvar.transform.SetParent(hitInfo.transform, true);
                    lookRadius = SeeRadiusSize;


                }
               
            }
        }