EnemyMover.cs 1.3 KB

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