using UnityEngine; public class PlayerMovement : MonoBehaviour { private Rigidbody2D playerBody; public float movementSpeed = 5f; public bool facingRight = true; private Animator animator; public float jumpPower = 8f; public GroundCollider groundCollider; private float horizontalInput; private AudioManager audioManager; public bool IsDead { get; set; } // Add this property void Start() { playerBody = gameObject.GetComponent(); animator = gameObject.GetComponent(); if (groundCollider == null) { Debug.LogError("GroundCollider is not assigned."); } } private void Awake() { audioManager = GameObject.FindGameObjectWithTag("Audio")?.GetComponent(); } void Update() { if (IsDead) { return; // If the player is dead, do nothing } if (animator != null) { animator.SetFloat("movement", Mathf.Abs(playerBody.velocity.x)); } if (groundCollider != null) { animator.SetBool("isGrounded", groundCollider.isGrounded); } } public void Move(float horizontalInput) { if (IsDead) { return; // If the player is dead, do nothing } if (horizontalInput != 0f) { CheckFacingDirection(horizontalInput); float horizontalVelocity = horizontalInput * movementSpeed; playerBody.velocity = new Vector2(horizontalVelocity, playerBody.velocity.y); } } private void CheckFacingDirection(float horizontalInput) { if (facingRight && horizontalInput < 0f) { Flip(); } else if (!facingRight && horizontalInput > 0f) { Flip(); } } private void Flip() { var playerScale = transform.localScale; transform.localScale = new Vector3(playerScale.x * -1, playerScale.y, playerScale.z); facingRight = !facingRight; } public void Jump() { if (IsDead) { return; // If the player is dead, do nothing } if (audioManager != null) { audioManager.PlaySFX(audioManager.jump); } if (groundCollider != null && groundCollider.isGrounded) { playerBody.AddForce(new Vector2(0f, jumpPower)); } } // Remove or modify this method if it was restricting attack during movement public bool canAttack() { return true; // Allow attacking regardless of movement or grounded status } }