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>(); Teo player = GameObject.FindGameObjectWithTag("Player").GetComponent<Teo>();
switch (upgrade) { switch (upgrade) {
case UpgradeOption.AttackRange: case UpgradeOption.AttackRange:
player.swingRange += amount; player.swingRangeDeg += amount;
break; break;
case UpgradeOption.AttackDistance: case UpgradeOption.AttackDistance:
player.swingDistance += amount; player.swingDistance += amount;

View File

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

View File

@@ -1046,7 +1046,7 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
upgrade: 0 upgrade: 0
amount: 1 amount: 1.5
--- !u!1 &3627401992951871319 --- !u!1 &3627401992951871319
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0