My Raycast2D doesn't work properly
This is about a stealth game. I have an enemy who patrols with given patrol points. I was trying to make the enemy can't see my character through the wall and stop the enemy itself, chasing the character by moving towards the wall if I was in its chase radius. Basically here, I have a problem with my enemy's radius and the Raycast of the enemy itself. I wanted to make the enemy to keep patrolling if the enemy doesn't see my character. So I was thinking to make it work, I guess I have to add some boolean to it to solve it. But, it doesn't work.
 void Start()
     {
         myRigidBody = GetComponent<Rigidbody2D>();
         killerAnim = GetComponent<Animator>();
         initialPosition = transform.position;
         target = GameObject.FindWithTag("Player").transform;
     }
 
     void Update()
     { 
         Vector3 Raycast = initialPosition;
         RaycastHit2D hit = Physics2D.Raycast(transform.position, target.transform.position - transform.position,
                            chaseRadius, 1 << LayerMask.NameToLayer("Default"));
 
         Vector3 forward = transform.TransformDirection(target.transform.position - transform.position);
 
         if (hit.collider != null)
         {
             if (hit.collider.tag == "Player")
             {
                 chase = true;
                 Debug.DrawRay(transform.position, forward, Color.green);
             }
             else {
                 chase = false;
                 Debug.DrawRay(transform.position, forward, Color.red);
             }
         }
         // chase radius to trigger the chase to move towards the character
         if (Vector3.Distance(target.position,
             transform.position) <= chaseRadius
             && Vector3.Distance(target.position, transform.position) > attackRadius && chase == true)
         {
             if (currentState == EnemyState.walk)
             {
                 Vector3 temp = Vector3.MoveTowards(transform.position, target.position, moveSpeed * Time.deltaTime);
                 changeAnim(temp - transform.position);
                 myRigidBody.MovePosition(temp);
                 ChangeState(EnemyState.walk);
                 killerAnim.SetBool("walking", true);
             }
         }
         // if the character is out of the chase radius, the enemy moving back to its patrol points and begin to patrol again
         else if (Vector3.Distance(target.position,
             transform.position) > chaseRadius && chase == false)
         {
             if (Vector3.Distance(transform.position, path[currPoint].position) > roundingDistance)
             {
                 Vector3 temp = Vector3.MoveTowards(transform.position, path[currPoint].position, moveSpeed * Time.deltaTime);
                 changeAnim(temp - transform.position);
                 myRigidBody.MovePosition(temp);
                 ChangeState(EnemyState.walk);
                 killerAnim.SetBool("walking", true);
             }
             else
             {
                 ChangeGoal();
             }
         }
         // this is the attack radius. It's going to be triggered if the enemy is close enough with the character
         else if (Vector3.Distance(target.position,
                  transform.position) <= chaseRadius
                  && Vector3.Distance(target.position, transform.position) <= attackRadius)
         {
             if (currentState == EnemyState.walk)
             {
                 StartCoroutine(attack());
             }
         }
     }
 
               I don't know where's the problem from my code. First of all, I'm sorry I'm not an advanced programmer. I'm just a beginner. This is also my first time using a Raycast. Any help will be so much appreciated... Thanks for your help.
Your answer
 
             Follow this Question
Related Questions
Raycast Enemy AI shooting script 1 Answer
Raycast2d does not always work properly 0 Answers
Unity Raycast Problems 1 Answer
Enemy walking off platform in Unity2D 0 Answers
Checking for Raycast distances not working as expected. 0 Answers