public override void DoBehavior(CharacterAttackController controller) { Vector2 target = Camera.main.ScreenToWorldPoint(Input.mousePosition); if (ShouldUseWeapon()) { controller.Attack(target); } }
public override void DoBehavior(CharacterAttackController controller) { List <Collider2D> possibleTargets = new List <Collider2D>(); Vector2 selfPosition = controller.transform.position; Team selfTeam = controller.GetComponent <TeamAssigner>().team; foreach (Collider2D collider in Physics2D.OverlapCircleAll(selfPosition, attackRadius)) { TeamAssigner assigner = collider.GetComponent <TeamAssigner>(); if (assigner != null && selfTeam != assigner.team) { possibleTargets.Add(collider); } } float closestDistance = Mathf.Infinity; Collider2D closestTarget = null; foreach (Collider2D possibleTarget in possibleTargets) { // Check if we have line of sight Vector2 targetPosition = possibleTarget.transform.position; Vector2 direction = targetPosition - selfPosition; float distance = direction.magnitude; bool lineOfSight = true; foreach (RaycastHit2D hit in Physics2D.RaycastAll(controller.transform.position, direction, distance)) { if (hit.collider.gameObject.GetComponent <VisionOccluder>() != null) { lineOfSight = false; } } if (lineOfSight && distance <= closestDistance) { closestTarget = possibleTarget; closestDistance = distance; } } if (closestTarget != null) { controller.Attack(closestTarget.transform.position); } }