using Unity.VisualScripting; using UnityEngine; using UnityEngine.InputSystem; public class PlayerMovement : MonoBehaviour { float runSpeed = 2.5f; Vector2 moveInput; //Creating a reference/variable for player inputs Rigidbody2D playerRigidbody; Animator playerAnimator; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { playerRigidbody = GetComponent(); playerAnimator = GetComponent(); } // Update is called once per frame void Update() { Run(); FlipSprite(); } void OnMove(InputValue value) //This method gets called when player presses movement keys { moveInput = value.Get(); } void Run() //Method that sorts out player movement { Vector2 playerVelocity = moveInput * runSpeed; //Changing player speed based on runSpeed playerRigidbody.linearVelocity = playerVelocity; //Moving the player bool playerHasSpeed = Mathf.Abs(playerRigidbody.linearVelocity.magnitude) > Mathf.Epsilon; //Checking if the player is moving left,right,up,down or is stationary playerAnimator.SetBool("isRunning", playerHasSpeed); //Change character animation state based on if the player is moving left or right, up or down } void FlipSprite() { bool playerHasHorizontalSpeed = Mathf.Abs(playerRigidbody.linearVelocity.x) > Mathf.Epsilon; //Checking if the player is moving left,right or is stationary if (playerHasHorizontalSpeed) //Run this if the player is moving { transform.localScale = new Vector2(Mathf.Sign(playerRigidbody.linearVelocity.x), 1f); } } }