using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyPathfinding : MonoBehaviour { [SerializeField] private float moveSpeed = 2f; // Speed at which the enemy moves private Rigidbody2D rb; // Reference to the Rigidbody2D component for movement private Vector2 moveDir; // Direction in which the enemy should move private Knockback knockback; // Reference to the Knockback component private SpriteRenderer spriteRenderer; // Reference to the SpriteRenderer component for flipping the sprite private void Awake() { spriteRenderer = GetComponent(); // Get the SpriteRenderer component attached to the enemy knockback = GetComponent(); // Get the Knockback component attached to the enemy rb = GetComponent(); // Get the Rigidbody2D component attached to the enemy } private void FixedUpdate() { // If the enemy is currently being knocked back, don't move if (knockback.GettingKnockedBack) { return; } // Move the enemy in the direction specified by moveDir rb.MovePosition(rb.position + moveDir * (moveSpeed * Time.fixedDeltaTime)); // Flip the enemy's sprite based on the movement direction if (moveDir.x < 0) { spriteRenderer.flipX = true; // Face left } else if (moveDir.x > 0) { spriteRenderer.flipX = false; // Face right } } // Set the direction in which the enemy should move public void MoveTo(Vector2 targetPosition) { moveDir = targetPosition; } // Stop the enemy's movement public void StopMoving() { moveDir = Vector3.zero; // Set movement direction to zero to stop movement } }