using Unity.VisualScripting.FullSerializer; using UnityEngine; public class PlayerMovement : MonoBehaviour { [SerializeField] private float speed; [SerializeField] private LayerMask groundLayer; [SerializeField] private LayerMask wallLayer; private Rigidbody2D body; private Animator anim; private BoxCollider2D boxCollider; private float wallJumpCooldown; private void Awake() { // grab references for rigidbody & animatior from gameobject body = GetComponent(); anim = GetComponent(); boxCollider = GetComponent(); } private void Update() { float horizontalInput = Input.GetAxis("Horizontal"); // move body.velocity = new Vector2(horizontalInput * speed, body.velocity.y); // flip player when moving left-right if (horizontalInput > 0.01f) { transform.localScale = Vector3.one; } else if (horizontalInput < -0.01f) { transform.localScale = new Vector3(-1, 1, 1); } // wall jump logic if (wallJumpCooldown < 0.2f) { if (Input.GetKey(KeyCode.W) && isGrounded()) { Jump(); } body.velocity = new Vector2(horizontalInput * speed, body.velocity.y); if (onWall() && !isGrounded()) { body.gravityScale = 0; body.velocity = Vector2.zero; } } // set animator parameters anim.SetBool("run", horizontalInput != 0); anim.SetBool("grounded", isGrounded()); } private void Jump() { body.velocity = new Vector2(body.velocity.x, speed); anim.SetTrigger("jump"); } private void OnCollisionEnter2D(Collision2D collision) { } private bool isGrounded() { RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider.bounds.center, boxCollider.bounds.size, 0, Vector2.down, 0.1f, groundLayer); return raycastHit.collider != null; } private bool onWall() { RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider.bounds.center, boxCollider.bounds.size, 0, new Vector2(transform.localScale.x, 0), 0.1f, wallLayer); return raycastHit.collider != null; } }