Enemy AI. Enemies won't detect player. [SOLVED]

For the game I am making I want to make the enemies move and attack my themselves. Detecting the player is the problem. When I wrote the script it worked well, even after some changes I made to add other stuff (like allowing them to pick up weapons and choose what weapon to go to). Now, all of a sudden it just stopped working. For absolutely no reason. I have no idea what is wrong with it but any help would be appreciated. This is a college project and the deadline is 3 weeks away and I am quite anxious that I might not finish it in time. If I don’t fix it now it’s going to set me back a lot.
Here is the script:

using UnityEngine;
using System.Collections;

public class EnemyAI : MonoBehaviour {
    GameObject Player;
    public bool patrol = true, guard = false, clockwise=false;
    public bool moving = true;
    public bool pursuingPlayer = false, goingToLastLoc = false;
    Vector3 target;
    Rigidbody2D rid;
    public Vector3 playerLastPos;
    RaycastHit2D hit;
    float speed = 2.0f;
    int layerMask = 1<<8;


    ObjectManager obj;
    GameObject[] weapons;
    EnemyWeaponController ewc;
    public GameObject weaponToGoTo;
    public bool goingToWeapon = false;
    public bool hasGun = false;
    // Use this for initialization
    void Start () {
        Player = GameObject.FindGameObjectWithTag ("Player");
        playerLastPos = this.transform.position;
        obj = GameObject.FindGameObjectWithTag ("GameController").GetComponent<ObjectManager> ();
        ewc = this.GetComponent<EnemyWeaponController> ();
        // hit = Physics2D.Raycast(new Vector2(this.transform.position.x,this.transform.position.y), new Vector2(dir.x, dir.y));
        rid = this.GetComponent<Rigidbody2D> ();
        layerMask = ~layerMask;
    }
   
    // Update is called once per frame
    void Update () {

        if (PlayerHealth.dead == false) {
            movement ();
            playerDetect ();
            canEnemyFindWeapon ();
        } else {
            this.GetComponent<EnemyAnimate> ().enabled = false;
        }
    }

    void movement()
    {
        float dist = Vector3.Distance (Player.transform.position, this.transform.position);
        Vector3 dir = Player.transform.position - transform.position;
        hit = Physics2D.Raycast (new Vector2 (this.transform.position.x, this.transform.position.y), new Vector2 (dir.x, dir.y), dist, layerMask);
        Debug.DrawRay (transform.position, dir, Color.red);

        Vector3 fwt = this.transform.TransformDirection (Vector3.right);
        RaycastHit2D hit2 = Physics2D.Raycast (new Vector2 (this.transform.position.x, this.transform.position.y), new Vector2 (fwt.x, fwt.y), 0.8f, layerMask);

        Debug.DrawRay (new Vector2 (this.transform.position.x, this.transform.position.y), new Vector2 (fwt.x, fwt.y), Color.cyan);

        if(moving == true)
        {
            transform.Translate (Vector3.right * speed * Time.deltaTime);
        }

        if(patrol==true)
        {
            Debug.Log ("Patrolling normally");
            speed = 2.0f;

            if(hit2.collider != null)
            {
                //Debug.LogError(hit2.collider.tag);
                if (hit2.collider.gameObject.tag == "Wall")
                {
                    //Quaternion rot = this.transform.rotation;

                    if (clockwise == false) {
                        transform.Rotate (0, 0, 90);
                    } else {
                        transform.Rotate (0, 0, -90);
                    }
                }

            }
            if (weaponToGoTo != null) {
                patrol = false;
                goingToWeapon = true;
            }
        }

        if(pursuingPlayer==true)
        {
            Debug.Log ("Pursuing Player");
            speed = 3.0f;
            rid.transform.eulerAngles = new Vector3 (0, 0, Mathf.Atan2((playerLastPos.y - transform.position.y), (playerLastPos.x - transform.position.x)) * Mathf.Rad2Deg);

            if (hit.collider.gameObject.tag == "Player") {
                playerLastPos = Player.transform.position;
            }
        }

        if (goingToLastLoc == true) {
            Debug.Log ("Checking last known player location");
            speed = 2.3f;
            rid.transform.eulerAngles = new Vector3 (0, 0, Mathf.Atan2 ((playerLastPos.y - transform.position.y), (playerLastPos.x - transform.position.x)) * Mathf.Rad2Deg);
            if (Vector3.Distance (this.transform.position, playerLastPos) < 1.5f) {
                patrol = true;
                goingToLastLoc = false;
            }
        }

        if (goingToWeapon == true) {
            speed = 3.2f;
            rid.transform.eulerAngles = new Vector3 (0, 0, Mathf.Atan2 ((weaponToGoTo.transform.position.y - transform.position.y), (weaponToGoTo.transform.position.x - transform.position.x)) * Mathf.Rad2Deg);

            if (ewc.getCur () != null) {
                weaponToGoTo = null;
                patrol = true;
                goingToWeapon = false;
                pursuingPlayer = false;
                goingToLastLoc = false;
            }
            if (weaponToGoTo.active==false || weaponToGoTo==null)
            {
                weaponToGoTo = null;
                patrol = true;
                goingToWeapon = false;
                pursuingPlayer = false;
                goingToLastLoc = false;
            }
        }
    }

    void setWeaponToGoTo (GameObject weapon)
    {
        weaponToGoTo = weapon;
        goingToWeapon = true;
        patrol = false;
        pursuingPlayer = false;
        goingToLastLoc = false;
    }

    void canEnemyFindWeapon()
    {
        if (ewc.getCur() == null && weaponToGoTo == null && goingToWeapon==false) {
            weapons = obj.getWeapons ();
            for(int x = 0;x<weapons.Length;x++)
            {
                float distance = Vector3.Distance (this.transform.position, weapons [x].transform.position);
                if (distance < 10) {
                    Vector3 dir = weapons [x].transform.position - transform.position;
                    RaycastHit2D wepCheck = Physics2D.Raycast (new Vector2 (this.transform.position.x, this.transform.position.y), new Vector2 (dir.x, dir.y), distance, layerMask);
                    if(wepCheck.collider.gameObject.tag == "Weapon")
                    {
                        setWeaponToGoTo (weapons[x]);
                    }
                }
            }
        }
    }

    public void playerDetect()
    {
        Vector3 pos = this.transform.InverseTransformPoint (Player.transform.position);

        if (hit.collider != null)
        {
            if (hit.collider.gameObject.tag == "Player" && pos.x >1.2f && Vector3.Distance(this.transform.position,Player.transform.position) <9) {
                patrol = false;
                pursuingPlayer = true;
                goingToWeapon = false;
            } else {
                if(pursuingPlayer==true)
                {
                    goingToLastLoc = true;
                    pursuingPlayer = false;
                    goingToWeapon = false;
                }
            }
        }
    }
    public float getSpeed()
    {
        return speed;
    }
}

I’d suggest you start off making some debug tools. easiest to do is make a public GameObject target field and constantly set that to whatever you hit, or null if you don’t hit anything. then you can get realtime feedback as to what the enemies are actually seeing.

next if that doesn’t work start using Debug.Log and breakpoints and walk through and see where it suddenly doesn’t go the code path you expect

I didn’t analyze everything, but this stood out:
Vector3 dir = Player.transform.position - transform.position;
This is not a direction.
The direction would be (Player.transform.position - transform.position).normalized;

This might be what is causing an issue.

Technically its an offset, however Directions can be inferred from offsets without issue. and pretty sure those values are normalized anyway inside the Ray calls. My suspicion is that the ray is hitting something else that’s not a player (or hitting the wrong layer)

But thats all guesswork, hence why I suggested using some form of Debugging.

Well, that’s news to me. I thought we needed to normalize the value. You’re must be right because I found this thread:
https://forum.unity3d.com/threads/physics-raycast-direction-parameter-need-to-be-normalized.6624/

Thank you for replying. I will try your suggestions but for now I managed to find a backup I made with a previous version of the enemy. When I imported it into the main scene it worked just fine. And the script was the same, I made no changes.

like I mentioned. my suspicion was the layers. You raycast against a specific layer so either your player changed to a different layer and wasn’t getting hit, or a new object was using the same layer and was blocking the raycast