using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; public class PlayerMovement : MonoBehaviour { private Rigidbody2D rb; private SpriteRenderer SR; private Transform Gr; private Animator Ani; private Collider2D C2D; private Inputs Input; private InputAction Walk; private InputAction Jump; private InputAction Dash; private Vector2 Force; public LayerMask GroundLayer; public float Speed; public float JumpPwr; private void Awake() { Input = new Inputs(); rb = this.gameObject.GetComponent(); Gr = this.gameObject.transform.Find("GroundCheck").transform; Ani = this.gameObject.GetComponent(); SR = this.gameObject.GetComponent(); C2D = this.gameObject.GetComponent(); } private void OnEnable() { Walk = Input.Movement.wasd; Jump = Input.Movement.Jump; Dash = Input.Movement.Dash; Walk.Enable(); Jump.Enable(); Dash.Enable(); } private void OnDisable() { Walk.Disable(); Jump.Disable(); Dash.Disable(); } private void Update() { Jump.performed += _ => jump(); Force = Walk.ReadValue(); if(Force.x < 0) { SR.flipX = true; Ani.SetBool("Running", true); C2D.offset = new Vector2(4.5f, -21); } else if(Force.x > 0) { SR.flipX = false; Ani.SetBool("Running", true); C2D.offset = new Vector2(-4.5f, -21); } else { Ani.SetBool("Running", false); } } private void FixedUpdate() { rb.velocity = new Vector2(Force.x*Speed, rb.velocity.y); } private bool IsGrounded() { return Physics2D.OverlapBox(Gr.position, new Vector2(24, 1), 0f, GroundLayer); } private void jump() { if(IsGrounded()) { rb.velocity = new Vector2(rb.position.x, JumpPwr); } } }