How to fix my chasing/attacking code?
So how the code currently works is, the enemy will find the player and move towards him. When he finds him, he will stop and then start attacking. If the player moves away though, the enemy will stop attacking and just sit there until the player comes back into range. How can I fix it so that when the player moves out of range, the enemy starts chasing again and then attacks as normal?
  float moveSpeed = 3f;
 float rotationSpeed = 3f;
 float attackThreshold = 3f; //distance within which to attack
 float chaseThreshold = 10f; //distance within which to start chasing
 float giveUpThreshold = 20f; //distance beyond which AI gives up
 float attackRepeatTime = 1f; //time between attacks
 bool attacking = false;
 bool chasing = false;
 float attackTime;
 Transform target; //the enemy's target
 Transform myTransform; //current transform data of the enemy
 void Update()
 {
 //rotate to look at the player
         float distance = (target.position - myTransform.position).magnitude;
         if (chasing)
         {
             myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
         }
         //move towards the player
         if (chasing == true && attacking == false)
             myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
         //give up if too far away
         if (distance >= giveUpThreshold)
         {
             chasing = false;
         //    attacking = false;
         }
         //attack, if close enough, and if time is OK
         if (distance <= attackThreshold && Time.time >= attackTime) //if attacking we want to stop moving
         {
             //attack here
             bossAttack.Attack();
             attackTime = Time.time + attackRepeatTime;
             print("Attacking!");
             attacking = true;
           //  anim.SetTrigger("AutoAttack");
             chasing = false;
         }
         else
         {
             //not currently chasing.
             //start chasing if target comes close enough
             if (distance <= chaseThreshold)  //if he gets to chase, and then you move out of range again, he won't chase again. he will only attack if comes into range again
             {
                 chasing = true;
               //  attacking = false;
                 //   print("Chasing!");
             }
         }
     }
 
               I think that's all the necessary relevant code.
Your answer
 
             Follow this Question
Related Questions
Different jump height 1 Answer
Hey Guys How to make a script '**private Transform Waypoint;**', 0 Answers
Transform.position of object not the same as shown on the inspector 1 Answer
How could I make an object move from left to right and it will come back from right? 0 Answers
Move to object if collision is triggered 0 Answers