using UnityEngine; public class PlayerMovement : MonoBehaviour { [SerializeField] private float speed; private Rigidbody2D body; private Animator anim; private bool grounded; private bool enemyColliding; private void Awake() { body = GetComponent(); anim = GetComponent(); } private void Update() { float horizontalInput = Input.GetAxis("Horizontal"); body.velocity = new Vector2(Input.GetAxis("Horizontal") * speed, body.velocity.y); if(horizontalInput > 0.01f) transform.localScale = Vector3.one; else if(horizontalInput < -0.01f) transform.localScale = new Vector3(-1, 1, 1); if (Input.GetKey(KeyCode.Space) && grounded) Jump(); anim.SetBool("walking", horizontalInput != 0); anim.SetBool("grounded", grounded); } private void Jump() { body.velocity = new Vector2(body.velocity.x, speed); anim.SetTrigger("jump"); grounded = false; } private void OnCollisionEnter2D(Collision2D collision) { if(collision.gameObject.tag == "Ground") grounded = true; if(collision.gameObject.tag != "Ground") grounded = false; if(collision.gameObject.tag == "Enemy") Hurt(); if(collision.gameObject.tag != "Enemy") enemyColliding = false; } private void Hurt() { body.velocity = new Vector2(body.velocity.x, speed); anim.SetTrigger("hurt"); enemyColliding = true; } }