AI is not moving where it is supposed to?

So a while back I was working on some AI code and got it to work how I wanted, and then after using unity collaborate one time it broke my entire project. Now I am rebuilding it, and for some reason my AI is not working like it used to. (Idk if it has anything to do with the collborate issue but its broken now so) What it is supposed to do is move towards the player while your inrange, and when your not, wander randomely within a set area (kind of.) Now it doesnt seem to follow the player at all and just moves in a random direction.

Heres my code if you have any ideas:

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

public class GruntAI : MonoBehaviour {

public float moveSpeed;


public float shootSpeed;

private bool inRange;

public float patrolArea;


private Transform playerPosition;

private Vector2 randPosition;

private Rigidbody2D rb;

public GameObject shot;
private Transform thisPos;

private bool CanShoot;

private float bullets;
public float maxBullets;
public float burstSpeed;

private bool Restoring;

// Use this for initialization
void Start () {

    playerPosition = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();  // this gets the position of the player so the AI can use it 

    inRange = false;

    randPosition = new Vector2(Random.Range(patrolArea * -1, patrolArea), Random.Range(patrolArea * -1, patrolArea)); // this gets a random vector2 so the AI can use it to wander

    rb = GetComponent<Rigidbody2D>();
    InvokeRepeating("generatePosition", 0, 1); // starts the generate position class every second

    thisPos = GetComponent<Transform>();

    CanShoot = true;

    bullets = maxBullets;

    Restoring = false;

}

// Update is called once per frame
void Update () {

    if (inRange == true) // this checks if the okayer is in range of the AI and starts moving towards it
    {

       rb.velocity = (playerPosition.position - transform.position).normalized * moveSpeed * Time.deltaTime;

        if (CanShoot == true && bullets > 0)
        {
            CanShoot = false;
            bullets = bullets - 1;
            Instantiate(shot, thisPos.position, Quaternion.identity);
            StartCoroutine(reload());

        }

    } else
    {

        rb.velocity = (new Vector3(randPosition.x, randPosition.y, 0) - transform.position).normalized * moveSpeed / 2 * Time.deltaTime; // otherwise constantly moves towards a new random position
    }

    
    if (bullets < 1 && Restoring == false)
    {
        Restoring = true;
        StartCoroutine(restoreBullets());
    }
    
    
}

private void OnTriggerEnter2D(Collider2D other) // checks if player is in range
{
    if (other.tag == "Player")
    {

    inRange = true;

    }
}

private void OnTriggerExit2D(Collider2D other) // checks if player is out of range
{
    if (other.tag == "Player")
    {
        inRange = false;

    }
}

public void generatePosition()
{
    randPosition = new Vector2(Random.Range(-patrolArea, patrolArea), Random.Range(-patrolArea, patrolArea));
}

IEnumerator reload() // the amount of time in between each shot
{
    yield return new WaitForSeconds(shootSpeed);
    CanShoot = true;
}

IEnumerator restoreBullets() // the class that reloads the bullets once they run out
{

    
    yield return new WaitForSeconds(burstSpeed);
    Restoring = false;

    bullets = maxBullets;
    
}

}

If you haven’t yet, put a bunch of debug prints inside your ontrigger events to see if it’s doing what you expect.

Whenever I don’t know where something is broken, that’s my default. Just fill in a bunch of Debug.Log() statements throughout your code at key sections and test to see what is being hit and what isn’t.