using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; public class PlayerMovementScript : MonoBehaviour { [SerializeField] float runSpeed = 10f; [SerializeField] float jumpSpeed =5f; Vector2 moveInput; Rigidbody2D myRigidbody; Animator myAnimator; CapsuleCollider2D myCapsuleCollider; void Start() { myRigidbody = GetComponent(); myAnimator = GetComponent(); myCapsuleCollider = GetComponent(); } void Update() { Run(); FlipSprite(); } void OnMove(InputValue value) { moveInput = value.Get(); Debug.Log(moveInput); } void OnJump(InputValue value) { if(!myCapsuleCollider.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("isrunning", playerHasHorizontalSpeed); } void FlipSprite() { bool playerHasHorizontalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon; if (playerHasHorizontalSpeed) { transform.localScale = new Vector2 (Mathf.Sign(myRigidbody.velocity.x), 1f); } } }