using System.Collections; using System.Collections.Generic; 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; // 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() { Run(); FlipSprite(); } void OnMove(InputValue value) { moveInput = value.Get(); Debug.Log(moveInput); } void OnJump(InputValue value) { if (!myBoxCollider.IsTouchingLayers(LayerMask.GetMask("Ground"))) { return;} if(value.isPressed) { // do stuff 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); } } }