using System.Collections; using System.Collections.Generic; using System.Security.Cryptography; using UnityEngine; public class Character : MonoBehaviour { [SerializeField] private float speed = 3f; //скорость персонажа [SerializeField] private int lives = 5; //здоровье персонажа [SerializeField] private float jumpForce = 1.5f; //сила прыжка персонажа private bool isGrounded = false; private Rigidbody2D rb; private Animator anim; private SpriteRenderer sprite; private States State { get { return (States)anim.GetInteger("state"); } set { anim.SetInteger("state", (int)value); } } private void Awake() { rb = GetComponent(); anim = GetComponent(); sprite = GetComponentInChildren(); } private void Update() { if (isGrounded) State = States.idle; if (Input.GetButton("Horizontal")) Run(); if (Input.GetButtonDown("Jump")) Jump(); } private void Run() { if (isGrounded) State = States.go; Vector3 dir = transform.right * Input.GetAxis("Horizontal"); transform.position = Vector3.MoveTowards(transform.position, transform.position + dir, speed * Time.deltaTime); sprite.flipX = dir.x < 0.0f; } private void Jump() { rb.AddForce(transform.up * jumpForce, ForceMode2D.Impulse); } private void CheckGround() { Collider2D[] collider = Physics2D.OverlapCircleAll(transform.position, 0.3f); isGrounded= collider.Length > 1; if (!isGrounded) State = States.jump; } } public enum States { idle, go, jump }