Загрузка данных


using System.Collections;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    [Header("Movement")]
    public float moveSpeed = 5f;
    public Rigidbody2D rb;
    public Animator animator;
    public SpriteRenderer spriteRenderer; // Ссылка на рендерер спрайта

    [Header("Dash")]
    public float dashSpeed = 15f;
    public float dashDuration = 0.2f;
    public float dashCooldown = 1f;
    private bool isDashing;
    private bool canDash = true;

    private Vector2 moveInput;

    void Update()
    {
        if (isDashing) return;

        // Ввод движения
        moveInput.x = Input.GetAxisRaw("Horizontal");
        moveInput.y = Input.GetAxisRaw("Vertical");

        // --- ЛОГИКА ОТЗЕРКАЛИВАНИЯ ---
        // Если движемся вправо (или стоим, но смотрели вправо)
        if (moveInput.x > 0.1f)
        {
            spriteRenderer.flipX = false; // Обычное состояние (смотрит вправо)
        }
        // Если движемся влево
        else if (moveInput.x < -0.1f)
        {
            spriteRenderer.flipX = true; // Отзеркалить (смотрит влево)
        }
        // *Если moveInput.x равен 0, спрайт сохраняет последнее состояние
        // ---------------------------------


        // Передаем параметры в аниматор
        if (moveInput != Vector2.zero)
        {
            animator.SetFloat("Horizontal", moveInput.x);
            animator.SetFloat("Vertical", moveInput.y);
        }

        // Скорость для перехода из Idle в Run
        animator.SetFloat("Speed", moveInput.sqrMagnitude);

        // Рывок на Shift
        if (Input.GetKeyDown(KeyCode.LeftShift) && canDash && moveInput != Vector2.zero)
            StartCoroutine(Dash());
    }

    void FixedUpdate()
    {
        if (isDashing) return;

        // Движение
        rb.MovePosition(rb.position + moveInput.normalized * moveSpeed * Time.fixedDeltaTime);
    }

    private IEnumerator Dash()
    {
        canDash = false;
        isDashing = true;

        rb.linearVelocity = moveInput.normalized * dashSpeed;
        yield return new WaitForSeconds(dashDuration);

        isDashing = false;
        rb.linearVelocity = Vector2.zero;

        yield return new WaitForSeconds(dashCooldown);
        canDash = true;
    }
}