public override void Execute() { entity.Rotate(entity.target.position - entity.transform.position, entity.chaseSpeed); if (Time.time > entity.nextAttack) { // First check if the target is still visible. if (entity.LookForTarget() == false) { EnemySearchState newState = new EnemySearchState(entity.transform.position, entity.target.position - entity.transform.position); entity.stateMachine.ChangeState(newState); } else if (Vector3.Distance(entity.transform.position, entity.target.position) > entity.attackRange) { // If the target is out of the attack range, look for it again. EnemySearchState newState = new EnemySearchState(entity.transform.position, entity.target.position - entity.transform.position); entity.stateMachine.ChangeState(newState); } else { entity.Attack(); entity.nextAttack = Time.time + entity.attackRate; } } }
public override void Execute() { entity.Move(entity.target.position - entity.transform.position, entity.chaseSpeed); if (Time.time > timeToCheck) { if (entity.LookForTarget() == false) { // Enter seach state. EnemySearchState newState = new EnemySearchState(lastTargetPosition + entity.transform.forward * 2.0f, entity.target.position - lastTargetPosition); entity.stateMachine.ChangeState(newState); } else { if (Vector3.Distance(entity.transform.position, entity.target.position) < entity.attackRange - entity.controller.radius) { EnemyAttackState newState = new EnemyAttackState(); entity.stateMachine.ChangeState(newState); } else { lastTargetPosition = entity.target.position; timeToCheck = Time.time + 0.2f; // Check with more frecuency, because the enemy is chasing. } } } }
private static EnemySearchState _instance; //only declared once private EnemySearchState() //set instance to itself { if (_instance != null) { return; } _instance = this; }
public override void Alert(Vector3 alertSource) { // Search the source of the alert... if (stateMachine.currentState is EnemyPatrolState) { if (alertSource == Vector3.zero) // This call is done from TakeDamage(). { alertSource = target.position; } EnemySearchState newState = new EnemySearchState(alertSource + transform.forward * 2.0f, transform.forward); stateMachine.ChangeState(newState); } }