Ratbots deal damage to the player upon contact

This commit is contained in:
2024-10-19 19:41:22 -07:00
parent 643070aefb
commit b10d75ac29
4 changed files with 18 additions and 5 deletions

View File

@@ -3,7 +3,10 @@ using UnityEngine;
public class Ratbot : MonoBehaviour
{
public float moveSpeed;
public float damageStrength;
public float damageTimeoutSeconds;
GameObject player;
float lastDamageTime;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
@@ -17,4 +20,11 @@ public class Ratbot : MonoBehaviour
Vector2 direction = (player.transform.position - transform.position).normalized;
controller.linearVelocity = direction*moveSpeed;
}
void OnCollisionStay2D(Collision2D collision) {
// Deal damage when colliding with the player on a 0.5 second cooldown
if (collision.gameObject.tag != "Player") return;
if (lastDamageTime > Time.fixedTime-damageTimeoutSeconds) return;
lastDamageTime = Time.fixedTime;
collision.gameObject.GetComponent<Teo>().health -= damageStrength;
}
}