public IEnumerator DamageEnemy(int incoming_damage) //first will apply damage, and then stagger the enemy for a certain duration { animator.Play("Stagger"); //apply damage and checks if the enemy dies from the damage. enemy_health -= incoming_damage; //Debug.Log("POOP"); //set the bools to allow knockback and prevent actions/movements. getting_knockback = true; knockback_force += incoming_damage; //Debug.Log (knockback_force); //create damage effect particles if (!player_countering) { GameObject effect = Instantiate(damaged_effect, transform.position, transform.rotation); Destroy(effect, 1f); } //change the AI state to staggered for animations. current_state = ai_state.staggered; //set staggering to true to affect fixedupdate to prevent the ai from doing any actions. staggering = true; //set to false to stop the AI from following the player with the navmesh. alerted = false; //stop the ai from moving by setting the acceleration, speed, and velocity to 0. transform.GetComponent <UnityEngine.AI.NavMeshAgent>().acceleration = 0; transform.GetComponent <UnityEngine.AI.NavMeshAgent>().speed = 0; transform.GetComponent <UnityEngine.AI.NavMeshAgent>().velocity = Vector3.zero; player_countering = false; yield return(new WaitForSeconds(0.01f)); }
private void AttackOnRange() { if (!PlayerInRange(5f, true)) //Change boolean to true for OnSight aggro / 1f-5f { state = ai_state.chase; return; } //Insert attack behavior }
private void GroundPatrol(ref Vector2 input) { //Switch to chase if player in range if (PlayerInRange(30f, true)) //Change boolean to true for OnSight aggro / 6f-30f { state = ai_state.chase; return; } //Coroutine for changing targeted platform if (_timerChangeTarget == false) { StartCoroutine(TimerForSwitchTarget()); } }
private void Chase() { //Add this method into AiController if (!PlayerInRange(30f, false)) //Change boolean to true for OnSight aggro / 6f-30f { state = ai_state.groundpatrol; return; } if (PlayerInRange(5f, true)) //Change boolean to true for OnSight aggro / 1f-5f { state = ai_state.attack; return; } _pathAgent.pathfindingTarget = player; state = ai_state.chase; }
public IEnumerator DamageEnemy() //first will apply damage, and then stagger the enemy for a certain duration { //apply damage and checks if the enemy dies from the damage. enemy_health -= incoming_damage; attack_script.staggered = true; //if they aren't dead, then stagger the basic enemy if (enemy_health > 0) { current_state = ai_state.staggered; staggering = true; yield return(new WaitForSeconds(stagger_duration)); //after the duration is over, set the enemy back up for attacking again. staggering = false; attack_script.staggered = false; alerted = true; current_state = ai_state.idle; } }
void FixedUpdate() { if (staggering) { current_stagger_dur += Time.deltaTime; //move the ai state to staggered, and set alerted to false to stop their movement. current_state = ai_state.staggered; alerted = false; //get knocked backwards. if (getting_knockback) { transform.Translate(backward_dir * knockback_force * Time.smoothDeltaTime, Space.Self); } //once the current_stagger_dur is done with knockback, stop the knockback by turning off getting_knockback. if (current_stagger_dur >= knockback_duration && getting_knockback) { getting_knockback = false; knockback_force = base_knockback_force; } //once the stagger duration is up, restore all of the old values of the AI to move and attack. if (current_stagger_dur >= knockback_duration + stagger_after_knockback_dur) { transform.GetComponent <UnityEngine.AI.NavMeshAgent>().acceleration = acceleration; transform.GetComponent <UnityEngine.AI.NavMeshAgent>().speed = movement_speed; staggering = false; alerted = true; current_stagger_dur = 0; current_state = ai_state.idle; } } //it is not staggering so run through the usual routines. else { if (berserk_mode && !first_berserk) { chance_to_dodge = 15; berserk_flame1.SetActive(true); berserk_mode = false; first_berserk = true; } distance_to_player = Vector3.Distance(target.position, transform.position); //calculate distance to player //used for checking and testing purposes. if (check_to_damage) { StartCoroutine("DamageEnemy", 11); check_to_damage = false; } if (distance_to_player < 30 && !first_alert) //if the player is close enough, this will set the AI to be alerted. //if the enemy is alerted then it will chase the player. AKA aggro range { alerted = true; first_alert = true; } //if the enemy reached 0 hp, it is dead so it will be put into the dying state then go through the death coroutine. if (enemy_health <= 0) { current_state = ai_state.dying; StartCoroutine("Death"); } if (distance_to_player <= 20) //if the player is close, this will keep the AI rotated towards the player { direction = (target.position - transform.position).normalized; look_rotation = Quaternion.LookRotation(direction); transform.rotation = Quaternion.Slerp(transform.rotation, look_rotation, Time.deltaTime * turn_speed); } if (getting_knockback) { transform.Translate(backward_dir * moveback_speed * Time.smoothDeltaTime, Space.Self); } if (alerted && !performing_action && distance_to_player > 3f) //if the AI is aggro, then it will chase the player. { //telling this object to chase after the target's position, otherwise known as the player's position. animator.Play("ForwardWalk"); transform.GetComponent <UnityEngine.AI.NavMeshAgent>().destination = target.position; animator.SetBool("isMoving", true); } else { animator.SetBool("isMoving", false); } //if the ai is close enough and not already performing an action //then it will pick a random action to do //and then perform the selected action. if (distance_to_player <= 8f && !performing_action) { performing_action = true; //the AI is now doing an action, used to make sure it is only doing one action. //randomly choose between an movement or attack behavior, there is a 35% chance of being a movement behavior and 65% chance of an attack. //comment the line below to "control" the AI. action_selection = Random.Range(0, 100); if (action_selection <= chance_to_dodge) { ai_state current_state = (ai_state)Random.Range(2, 4); //randomly select to either dodge or moveback. switch (current_state) //based on the choice, do the corresponding coroutines { case ai_state.dodging: StartCoroutine("Dodge"); break; case ai_state.moveback: if (distance_to_player > 6f) { StartCoroutine("Dodge"); } else { StartCoroutine("MoveBack"); } break; }// at the end of each coroutines, it will set performing_action back to false to allow for a loop if the ai is still within range. } else { ai_state current_state = (ai_state)Random.Range(4, 7); //randomly select within the attack states. switch (current_state) //based on the choice, do the corresponding coroutines { case ai_state.attack_1: StartCoroutine("Attack_1"); break; case ai_state.attack_2: StartCoroutine("Attack_2"); break; case ai_state.attack_3: StartCoroutine("Attack_3"); break; } } } // the dodge works by moving towards transform positions of empty game objects that are attached to the ai. if (dodge == dodge_direction.left) //this will make the ai move to the left or right when they are in that dodge state. { transform.position = Vector3.Lerp(transform.position, left_dodge.transform.position, dodge_speed * Time.deltaTime); } else if (dodge == dodge_direction.right) { transform.position = Vector3.Lerp(transform.position, right_dodge.transform.position, dodge_speed * Time.deltaTime); } } }
void FixedUpdate() { distance_to_player = Vector3.Distance(target.position, transform.position); //calculate distance to player if (check_to_damage) //used for checking purposes. if the enemy is damaged then it must get stunned { StartCoroutine("DamageEnemy"); check_to_damage = false; } if (distance_to_player < 50 && !first_alert) //if the player is close enough, this will set the AI to be alerted. //if the enemy is alerted then it will chase the player. AKA aggro range { alerted = true; first_alert = true; } //if the ai is staggered, then dont do anything for this frame. if (staggering) { current_state = ai_state.staggered; alerted = false; //Debug.Log ("staggering"); return; } if (enemy_health <= 0) { current_state = ai_state.dying; StartCoroutine("Death"); } if (distance_to_player <= 20) //if the player is close, this will keep the AI rotated towards the player { direction = (target.position - transform.position).normalized; look_rotation = Quaternion.LookRotation(direction); transform.rotation = Quaternion.Slerp(transform.rotation, look_rotation, Time.deltaTime * turn_speed); //legacy code below for a really good aim bot. /* * Vector3 look_pos = target.position; * look_pos.y = transform.position.y; * Quaternion rotate = Quaternion.LookRotation(look_pos - transform.position); * transform.rotation = rotate; */ } if (alerted) //if the AI is aggro, then it will chase the player. { //telling this object to chase after the target's position. transform.GetComponent <UnityEngine.AI.NavMeshAgent>().destination = target.position; } if (distance_to_player <= 5.5f && !performing_action) { //if the ai is close enough and not already performing an action, then it will pick a random action to do //and then perform the selected action. performing_action = true; //the AI is now doing an action ai_state current_state = (ai_state)Random.Range(2, 6); //will pick from dodge through attack 3 switch (current_state) //based on the choice, do the corresponding coroutines { case ai_state.dodging: StartCoroutine("Dodge"); break; case ai_state.attack_1: StartCoroutine("Attack_1"); break; case ai_state.attack_2: StartCoroutine("Attack_2"); break; case ai_state.attack_3: StartCoroutine("Attack_3"); break; }// at the end of each coroutines, it will set performing_action back to false to allow for a loop if the ai is still within range. } if (dodge == dodge_direction.left) //this will make the ai move to the left or right when they are in that dodge state. { transform.position = Vector3.Lerp(transform.position, left_dodge.transform.position, 1f * Time.deltaTime); } else if (dodge == dodge_direction.right) { transform.position = Vector3.Lerp(transform.position, right_dodge.transform.position, 1f * Time.deltaTime); } // the dodge works by moving towards transform positions of empty game objects that are attached to the ai. }