using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlatformerLayerMask2D : MonoBehaviour { public float speed = 8; public float jumpPower = 24; [SerializeField] private Transform groundCheck; [SerializeField] private LayerMask groundLayer; private Rigidbody2D rb; private float horizontal; private bool isFacingRight = true; void Start() { rb = GetComponent(); } void Update() { horizontal = Input.GetAxisRaw("Horizontal"); if (Input.GetButtonDown("Jump") == true && IsGrounded() == true) { rb.velocity = new Vector2(rb.velocity.x, jumpPower); } if (Input.GetButtonUp("Jump") == true && rb.velocity.y > 0f) { rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f); } Flip(); } void FixedUpdate() { rb.velocity = new Vector2(horizontal * speed, rb.velocity.y); } bool IsGrounded() { return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer); } void Flip() { if (isFacingRight == true && horizontal < 0f || isFacingRight == false && horizontal > 0f) { isFacingRight = !isFacingRight; Vector3 localScale = transform.localScale; localScale.x *= -1; transform.localScale = localScale; } } }