EnemyHeavy.cs 1.3 KB

123456789101112131415161718192021222324252627282930
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class EnemyHeavy : EnemyBase {
  5. internal override bool isPressingCrouch() => false;
  6. internal override bool isPressDownJump() => false;
  7. internal override bool isPressingLeft() => movingLeft;
  8. internal override bool isPressingRight() => !movingLeft;
  9. private float turnCooldown = 0f;
  10. internal override void OnUpdate() {
  11. bool shouldTurn =
  12. (movingLeft && Physics2D.Linecast(transform.position, transform.position + Vector3.left * 0.5f, LayerMask.GetMask("Marker"))) ||
  13. (!movingLeft && Physics2D.Linecast(transform.position, transform.position + Vector3.right * 0.5f, LayerMask.GetMask("Marker")));
  14. if (Mathf.Abs(rb.velocity.x) > 0.0001f && (rb.velocity.x > 0 == movingLeft)) shouldTurn = true;
  15. turnCooldown -= Time.deltaTime;
  16. if (shouldTurn && turnCooldown <= 0f) {
  17. movingLeft = !movingLeft;
  18. turnCooldown = 1f;
  19. }
  20. }
  21. public float knockbackForceH = 30f, knockbackForceV = 15f;
  22. internal override void OnHitEnemy(Collision2D collision) {
  23. int face = collision.transform.position.x < transform.position.x ? 1 : -1;
  24. rb.velocity = new Vector2(face * knockbackForceH, knockbackForceV);
  25. }
  26. }