public void Execute(Entity e) { AIEntity entity = (AIEntity)e; // check point 1 { // check runaway condition (life) if (entity.Ai.IsRunawayCondition()) { entity.Event = eEntityState.Runaway; return; } // check still in attack range ( not in sight) RaycastResult result = Facade_AI.IsRaycastHit(entity.property, entity.target, entity.property.tb.attackRange); if (result != RaycastResult.FoundTarget) { // replace target and mode change to attack Vector3 distance = entity.property.DistanceFrom(entity.target); if (distance.magnitude < entity.property.tb.chaseRange) { entity.Event = eEntityState.Chase; } else { entity.Event = eEntityState.Search; } return; } } // check point 2 { // attack processing if (elapsedTime > entity.property.tb.attack_interval) { elapsedTime = 0; // attack entity.Ai.OnAttack(entity.target); } if (entity.Ai.animator) { entity.Ai.animator.SetBool("attack", true); } // increase elapse time elapsedTime += Time.deltaTime; } float slerpTime = 10.0f; // look at the target Vector3 look = entity.property.DistanceFrom(entity.target).normalized; entity.property.transform.forward = Vector3.Lerp(entity.property.transform.forward, look, slerpTime * Time.deltaTime); }
public void Execute(Entity e) { AIEntity entity = (AIEntity)e; // check point 1 { // check runaway condition (life) if (entity.Ai.IsRunawayCondition()) { entity.Event = eEntityState.Runaway; return; } // check target in sight ( attack range) if (entity.target && entity.target.isAlive) { RaycastResult result = Facade_AI.IsRaycastHit(entity.property, entity.target, entity.property.tb.attackRange); if (result == RaycastResult.FoundTarget) { entity.Ai.agent.isStopped = true; entity.Event = eEntityState.Attack; return; } // blocked sight if (result == RaycastResult.BlockedTarget) { Vector3 distance = entity.property.DistanceFrom(entity.target); if (distance.magnitude < entity.property.tb.chaseRange) { entity.Ai.agent.destination = entity.target.transform.position; entity.Ai.agent.isStopped = false; return; } } } else { ObjectProperty target; if (Facade_AI.DetectTarget(entity.property, entity.property.tb.attackRange, out target)) { // replace target and mode change to attack entity.Ai.agent.isStopped = true; entity.target = target; entity.Event = eEntityState.Attack; return; } } } //check point 2 { // follow target if it is in chaseRange if (entity.target && entity.target.isAlive) { Vector3 distance = entity.property.DistanceFrom(entity.target); if (distance.magnitude < entity.property.tb.chaseRange) { entity.Ai.agent.destination = entity.target.transform.position; return; } } } // check reach them and something if (!entity.Ai.IsMoving) { entity.Event = eEntityState.Wander; } }