Hey! Thank you very much for your help! The problem was in the wrong angle
RaycastHit2D[] hits = Physics2D.BoxCastAll(transform.position, new Vector2(attackRangeX, attackRangeY), 0f, transform.right, 2f, whatIsEnemies);
private void OnDrawGizmosSelected() {
float angle = Util.GetAngleTowardsCursor(transform.position);
Quaternion rotation = Quaternion.Euler(new Vector3(0f, 0f, angle - 90f));
Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
Gizmos.DrawWireCube(Vector3.right * 2f, new Vector2(attackRangeX, attackRangeY));
}
public static float GetAngleTowardsCursor(Vector3 pos) {
Vector3 mousePos = Input.mousePosition;
mousePos.z = 5.23f;
Vector3 objectPos = Camera.main.WorldToScreenPoint(pos);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
return angle;
}