using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; public class PlayerMovement : MonoBehaviour { [SerializeField] float runSpeed = 5f; [SerializeField] float jumpspeed = 8f; Vector2 moveInput; Rigidbody2D myRigidbody; Animator myAnimator; BoxCollider2D myBoxCollider; bool isAlive = true; void Start() { myRigidbody = GetComponent(); myAnimator = GetComponent(); myBoxCollider = GetComponent(); } void Update() { if (isAlive) { Run(); FlipSprite(); } Die(); } void OnMove(InputValue value) { if (isAlive) { moveInput = value.Get(); Debug.Log(moveInput); } } void OnJump(InputValue value) { if (!myBoxCollider.IsTouchingLayers(LayerMask.GetMask("Ground"))) { return; } if (value.isPressed) { myRigidbody.velocity += new Vector2(0f, jumpspeed); } } void Run() { Vector2 playerVelocity = new Vector2(moveInput.x * runSpeed, myRigidbody.velocity.y); myRigidbody.velocity = playerVelocity; bool playerHasHorizontalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon; myAnimator.SetBool("isWalking", playerHasHorizontalSpeed); Debug.Log("Player Velocity: " + myRigidbody.velocity); } void FlipSprite() { bool playerHasHorizontalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon; if (playerHasHorizontalSpeed) { transform.localScale = new Vector2(Mathf.Sign(myRigidbody.velocity.x), 1f); } } void Die() { if (myBoxCollider.IsTouchingLayers(LayerMask.GetMask("Enemies"))) { isAlive = false; myAnimator.SetBool("isWalking", false); } } }