using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { public float speed; public float jumpForce; public float lowJumpMultiplier = 2f; public float fallMultiplier = 2.5f; Rigidbody2D rb; private Animator anim; private SpriteRenderer sr; private float moveBy; private bool isGrounded = false; private bool facingRight = true; // Start is called before the first frame update void Start() { rb = GetComponent(); anim = GetComponent(); sr = GetComponent(); } // Update is called once per frame void Update() { if (rb.velocity.y < 0) { rb.velocity += Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1) * Time.deltaTime; } else if (rb.velocity.y > 0 && !Input.GetButton("Jump")) { rb.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime; } Move(); Jump(); } void Move() { float x = Input.GetAxisRaw("Horizontal"); moveBy = x * speed; rb.velocity = new Vector2(moveBy, rb.velocity.y); AnimatePlayer(); } void Jump() { if (Input.GetKeyDown(KeyCode.Space) && isGrounded) { rb.velocity = new Vector2(rb.velocity.x, jumpForce); isGrounded = false; } } void AnimatePlayer() { if (moveBy > 0) { anim.SetBool("Run", true); if (!facingRight) { Flip(); } } else if (moveBy < 0) { anim.SetBool("Run", true); if (facingRight) { Flip(); } } else { anim.SetBool("Run", false); } } void Flip() { facingRight = !facingRight; transform.Rotate(0f, 180f, 0f); } private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Ground")) { isGrounded = true; } } }