Attack uses OverlapCastAll instead of CircleCastAll

Swing is more accurate now
This commit is contained in:
2024-10-25 13:24:47 -07:00
parent 2f8fbe8ce7
commit 18b5d96b79
3 changed files with 18 additions and 13 deletions

View File

@@ -21,7 +21,7 @@ public class ShopButton : MonoBehaviour
Teo player = GameObject.FindGameObjectWithTag("Player").GetComponent<Teo>();
switch (upgrade) {
case UpgradeOption.AttackRange:
player.swingRange += amount;
player.swingRangeDeg += amount;
break;
case UpgradeOption.AttackDistance:
player.swingDistance += amount;

View File

@@ -6,7 +6,7 @@ public class Teo : MonoBehaviour
public float health = 100;
public float maxHealth = 100;
public float moveSpeed = 10;
public float swingRange = 3;
public float swingRangeDeg = 3;
public float swingDistance = 10;
public float damageStrength = 10;
public float attackCooldownSeconds = 10;
@@ -47,20 +47,24 @@ public class Teo : MonoBehaviour
return;
}
Instantiate(distanceIndicator, transform);
// Get all objects in range in the direction of cursor
RaycastHit2D[] hits = Physics2D.CircleCastAll(
// Get all objects within swingDistance of player
Collider2D[] hits = Physics2D.OverlapCircleAll(
transform.position,
swingRange,
new Vector2(
Input.mousePosition.x - Screen.width/2,
Input.mousePosition.y - Screen.height/2
).normalized,
swingDistance
);
// Damage all ratbots in scanned area
foreach (RaycastHit2D hit in hits) {
Ratbot ratbot = hit.collider.GetComponent<Ratbot>();
if (ratbot == null) continue;
foreach (Collider2D hit in hits) {
Ratbot ratbot = hit.GetComponent<Ratbot>();
// If ratbot exists and is within swing range, damage it.
// Calculate absolute difference between angles of ratbot and cursor
if (ratbot == null || Mathf.Abs(Mathf.Atan2(
ratbot.transform.position.y - transform.position.y,
ratbot.transform.position.x - transform.position.x
) - Mathf.Atan2(
Screen.height/2 - Input.mousePosition.y,
Screen.width/2 - Input.mousePosition.x
)) > swingRangeDeg * Mathf.Deg2Rad
) continue;
ratbot.Damage(damageStrength);
}
}
@@ -75,6 +79,7 @@ public class Teo : MonoBehaviour
Input.mousePosition.x - Screen.width/2,
Input.mousePosition.y - Screen.height/2
).normalized * dashDistance);
// Wait 0.2 seconds before ending the dash
yield return new WaitForSeconds(0.2F);
dashing = false;
collider.enabled = true;