using System.Collections; using System.Collections.Generic; using Unity.VisualScripting; using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.SceneManagement; public class PlayerMovement : MonoBehaviour { [SerializeField] float runSpeed = 10f; [SerializeField] float JumpSpeed = 5f; Vector2 moveInput; Rigidbody2D myRigidbody; Animator myAnimator; CapsuleCollider2D myCapsulecollider; BoxCollider2D myFeetcollider; // Start is called before the first frame update void Start() { myRigidbody = GetComponent(); myAnimator = GetComponent(); myCapsulecollider = GetComponent(); myFeetcollider = GetComponent(); } // Update is called once per frame void Update() { if (!isAlive) {return;} Run(); FlipSprite(); Die(); } bool isAlive = true; void OnMove(InputValue value) { if (!isAlive) {return;} moveInput = value.Get(); } void OnJump(InputValue value) { if (!isAlive) {return;} if (!myFeetcollider.IsTouchingLayers(LayerMask.GetMask("Ground"))) { return; } if(value.isPressed) { myRigidbody.velocity += new Vector2 (0f, JumpSpeed); myAnimator.SetBool("IsJumping", true); } else { myAnimator.SetBool("IsJumping", false); // Set IsJumping to false when not jumping } } void Run() { if(moveInput.x * runSpeed != 0) { Vector2 playerVelocity = new Vector2(moveInput.x * runSpeed, myRigidbody.velocity.y); myRigidbody.velocity = playerVelocity; bool PlayerHasHorizontalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon; myAnimator.SetBool("IsRunning", PlayerHasHorizontalSpeed); } else { myAnimator.SetBool("IsRunning", false); } } 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 (myCapsulecollider.IsTouchingLayers(LayerMask.GetMask("Enemies"))) { isAlive = false; myAnimator.SetTrigger("Dying"); SceneManager.LoadScene("Lose"); } } }