using System.Collections; using System.Collections.Generic; using Unity.VisualScripting; using UnityEngine; using UnityEngine.InputSystem; public class PlayerMovement : MonoBehaviour { [SerializeField] float runSpeed = 5f; [SerializeField] float jumpSpeed = 8f; Vector2 moveInput; Rigidbody2D myRigidbody; Animator myAnimator; BoxCollider2D myBoxCollider; PolygonCollider2D myPolygonCollider2D; bool isAlive = true; // Start is called before the first frame update void Start() { myRigidbody = GetComponent(); myAnimator = GetComponent(); myBoxCollider = GetComponent(); myPolygonCollider2D = GetComponent(); } // Update is called once per frame void Update() { if (!isAlive) { return; } Run(); FlipSprite(); Die(); } void OnMove(InputValue value) { if (!isAlive) { return; } moveInput = value.Get(); Debug.Log(moveInput); } void OnJump(InputValue value) { if (!isAlive) { return; } if (!myBoxCollider.IsTouchingLayers(LayerMask.GetMask("Ground"))) { return; } if (value.isPressed) { myRigidbody.velocity += new Vector2(0f, jumpSpeed); } } void Run() { Vector2 playerVelocity = new Vector2(moveInput.x * runSpeed, myRigidbody.velocity.y); myRigidbody.velocity = playerVelocity; bool playerHasHorizontalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon; myAnimator.SetBool("isWalking", playerHasHorizontalSpeed); } void FlipSprite() { bool playerHasHorizontalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon; if (playerHasHorizontalSpeed) { transform.localScale = new Vector2(Mathf.Sign(myRigidbody.velocity.x), 1f); } } void Die() { if (myBoxCollider.IsTouchingLayers(LayerMask.GetMask("Enemies"))) { TriggerDeath(); } if (myBoxCollider.IsTouchingLayers(LayerMask.GetMask("Water"))) { TriggerDeath(); } } private void OnTriggerEnter2D(Collider2D other) { if (other.gameObject.layer == LayerMask.NameToLayer("Enemies")) { TriggerDeath(); } } void TriggerDeath() { if (!isAlive) return; isAlive = false; // Stop all player movement myRigidbody.velocity = Vector2.zero; // Trigger the death animation myAnimator.SetTrigger("isDead"); // Optionally, add logic for game over or respawning Debug.Log("Player has died."); } }