using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class PlayerController : MonoBehaviour { public float movementSpeed = 5f; public Rigidbody2D rb; Vector2 movement; private Animator animator; private bool FacingRight = true; private string currentAnimaton; const string PLAYER_IDLE = "Player_Idle"; const string PLAYER_WALK = "Player_Walk"; void Start() { animator = GetComponent(); } void Update() { movement.x = Input.GetAxisRaw("Horizontal"); movement.y = Input.GetAxisRaw("Vertical"); } void FixedUpdate() { rb.MovePosition(rb.position + movement * movementSpeed * Time.fixedDeltaTime); if (movement.x < 0 && FacingRight) { Flip(); } else if (movement.x > 0 && !FacingRight) { Flip(); } if ((movement.x != 0) || (movement.y != 0)) { ChangeAnimationState(PLAYER_WALK); } else { ChangeAnimationState(PLAYER_IDLE); } } void Flip() { FacingRight = !FacingRight; transform.Rotate(0f, 180f, 0f); } void OnBecameInvisible() { Debug.Log("You died"); SceneManager.LoadScene("GameOver"); } void ChangeAnimationState(string newAnimation) { if (currentAnimaton == newAnimation) return; animator.Play(newAnimation); currentAnimaton = newAnimation; } }