Attack cooldown works now

This commit is contained in:
2024-10-21 15:27:29 -07:00
parent 31ad45d6bd
commit bf1c9fe5da
10 changed files with 98 additions and 41 deletions

View File

@@ -8,12 +8,12 @@ public class Teo : MonoBehaviour
public float swingRange = 3;
public float swingDistance = 10;
public float damageStrength = 10;
public float attackCooldownSeconds = 2;
public float attackCooldownSeconds = 10;
public float dashDistance = 5;
public float dashCooldownSeconds = 5;
Rigidbody2D controller;
GameState gameState;
float lastAttackTime;
float lastAttackTime = 0;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start() {
controller = GetComponent<Rigidbody2D>();
@@ -29,22 +29,29 @@ public class Teo : MonoBehaviour
).normalized * moveSpeed;
// Attack on click if not on cooldown
if (Input.GetMouseButtonDown(0) && Time.fixedTime > lastAttackTime + attackCooldownSeconds) {
// Get all objects in range in the direction of cursor
RaycastHit2D[] hits = Physics2D.CircleCastAll(
transform.position,
swingRange,
new Vector2(
Screen.width/2 - Input.mousePosition.x,
Screen.height/2 - Input.mousePosition.y
)
);
// Damage all ratbots in scanned area
foreach (RaycastHit2D hit in hits) {
Ratbot ratbot = hit.collider.GetComponent<Ratbot>();
if (ratbot == null) continue;
ratbot.Damage(damageStrength);
}
if (Input.GetMouseButtonDown(0)) {
Attack();
lastAttackTime = Time.fixedTime;
}
}
void Attack() {
if (Time.fixedTime-attackCooldownSeconds > lastAttackTime) {
return;
}
// Get all objects in range in the direction of cursor
RaycastHit2D[] hits = Physics2D.CircleCastAll(
transform.position,
swingRange,
new Vector2(
Screen.width/2 - Input.mousePosition.x,
Screen.height/2 - Input.mousePosition.y
)
);
// Damage all ratbots in scanned area
foreach (RaycastHit2D hit in hits) {
Ratbot ratbot = hit.collider.GetComponent<Ratbot>();
if (ratbot == null) continue;
ratbot.Damage(damageStrength);
}
}
}