Enemy Pattern
Update on the Enemy AI.
Here’s a snippet to the main code of the AI, this will be on each enemy and will control how they all work.
#region MonoBehaviour Functions
private void Awake() { patrolState = new PatrolState(this, m_min_movement, m_max_movement); alertState = new AlertState(this); chaseState = new ChaseState(this); attackState = new AttackState(this); jumpAttackState = new JumpAttackState(this); rotateAttackState = new RotateAttackState(this); deathState = new DeathState(this);
m_camera = Camera.main.GetComponent<Transform>(); transform = GetComponent<Transform>(); navMeshAgent = GetComponent<NavMeshAgent>(); rigidbody = GetComponent<Rigidbody>(); }
// Use this for initialization void Start() {
m_original_heal_bar_scale = m_health_bar.localScale.x;
//enemySpawnManager = GetComponent<EnemySpawnManager>();
animator.SetBool("Is_Attacking", false); animator.SetBool("Is_Moving", true); transform.position = spawnLocationObject.position; currentState = patrolState;
// assigning right animation clip to the animator
//AnimatorOverrideController aoc = new AnimatorOverrideController(); //aoc.runtimeAnimatorController = animator.runtimeAnimatorController;
//// switch between animations based on the states
//switch (m_attacking_style) //{ // case AttackingStyle.CHASE_ATTACK: // aoc["Attacking"] = m_swiping; // break; // case AttackingStyle.JUMP_ATTACK: // Debug.Log("sabdjsd"); // aoc["Attacking"] = m_jump_attack; // break; //}
//animator.runtimeAnimatorController = aoc; }
// Update is called once per frame void Update() {
UpdateUI();
if (currentState == deathState) m_is_dead = true;
Debug.Log("Current State Is " + currentState);
if (m_health <= 0f) currentState = deathState;
currentState.UpdateState();
//rigidbody.AddForce(transform.forward * m_jump_force); }
private void OnTriggerEnter(Collider other) { currentState.OnTriggerEnter(other);
if (!check_first_collision) { CharacterControl c = other.GetComponent<CharacterControl>(); if (c != null) { characterControl = c;
m_projectile.CreatePool(); m_projectile.m_Player = characterControl.MPlayerTransform; check_first_collision = true; } } }
#endregion
#region Custom Functions
void SmoothLook(Vector3 newDirection) { transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(newDirection), Time.deltaTime); }
public void EnableCollisionBox() {
attackingObject.SetActive(true);
}
public void DisableCollisionBox() { //Debug.Log("This is being called"); attackingObject.SetActive(false);
}
public void ApplyJumpForce() { //rigidbody.AddForce(transform.forward * m_jump_force);
Vector3 dir = chaseTarget.position - transform.position; dir = dir.normalized;
navMeshAgent.SetDestination(transform.position); }
public void EnterJumpAttackAnimation() { MJumpAttack = true; } public void ExitJumpAttackAnimation() { MJumpAttack = false; }
public void AssignSpawner(EnemySpawnManager spawner) { enemySpawnManager = spawner; }
public IEnumerator DeathWait() { yield return new WaitForSeconds(enemySpawnManager.cooldown);
this.Recycle();
ResetStats(); ResetState(); }
public void RecycleEnemy() { StartCoroutine(DeathWait()); }
void ResetState() { currentState = patrolState; }
void UpdateUI() { m_back_health_bar.LookAt(m_camera);
float x = Mathf.Lerp(0, m_original_heal_bar_scale, GetHealthPercentage()); m_health_bar.localScale = new Vector3(x, m_health_bar.localScale.y, m_health_bar.localScale.z); }
#endregion
}
Some parts are commented out since I’m still working on them and aren’t working currently.
It’s still definitely way more easier to understand then my Companion code, might change that later.














