/
RealMotya
/
Platformer
Обзор
Документация
Войти
/
RealMotya
/
Platformer
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Assets/_Scripts/Player.cs
204 строки
5 KB
VR4\IT-Cube28
Fear: добавил босса
29 янв 2026, 10:37
29 янв 2026, 10:37
ebc6168
Код
Авторство
О чём код?
using System.Runtime.CompilerServices; using TMPro; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public enum state { Idle = 0, Run = 1, Jump = 2, } public class Player : Character { public float Speed = 100f; public float JumpPower = 15f; public float Speed2 = 1f; public Transform CheckPoint; private Rigidbody2D _rb; //������ �� ��������� Rigidbody2D private Animator _animator; private SpriteRenderer _spriteRenderer; public Image HealthImage; public GameObject LossPanel; private float _dir; // ���������� ��� �������� �������� ����������� �������������� ��� (������� �����/������ � A/D) private bool _isGrounded = false; private state _state = state.Idle; private int _coin; public TextMeshProUGUI CoinText; private new void Start() { base.Start(); _rb = GetComponent<Rigidbody2D>();//������� ������ �� ��������� Rigidbody2D _animator = GetComponentInChildren<Animator>(); _spriteRenderer = GetComponentInChildren<SpriteRenderer>(); SetPlayerData(); } void Update() { Move(); Jump(); SetAnimtion(); } public void SetPlayerData() { //�������� ������ ������. if (SaveManager.IsContinue) { _health = SaveManager.LoadData.Health; _coin = SaveManager.LoadData.Coins; transform.position = new Vector2(SaveManager.LoadData.PositionX, SaveManager.LoadData.PositionY); CoinText.text = _coin.ToString(); HealthImage.fillAmount = (float)_health / _startHealth; } } public int GetHealth() => _health; public int GetCoins() => _coin; private void Move() { if (_dir == 0) { if (_state != state.Jump) { _state = state.Idle; } } else { _state = state.Run; } _spriteRenderer.flipX = _dir < 0; //����� GetAxis ���������� �������� -1 ���� ������ ������� ����� (���� A) //��� 1 ���� ������ ������� ������ (���� D) //���� ������� �� ������ �� ������ 0 _dir = Input.GetAxis("Horizontal"); } private void SetAnimtion() { _animator.SetInteger("state", (int) _state); } private void FixedUpdate() { _isGrounded = ChekGround(); //������ �������� �� ��� X //_rb.linearVelocityX = _dir * Time.fixedDeltaTime * Speed; if (_dir != 0) { _rb.AddForceX(_dir * Speed); } if (_rb.linearVelocityX > Speed2) { _rb.linearVelocityX = Speed2; } else if (_rb.linearVelocityX < -Speed2) { _rb.linearVelocityX = -Speed2; } } private void Jump() { if (Input.GetButtonDown("Jump") == true) { if (_isGrounded == true) { _rb.AddForce(Vector2.up * JumpPower); } } } public new void GetDamage(int damage) { base.GetDamage(damage); _animator.SetTrigger("morganie"); HealthImage.fillAmount = (float)_health / _startHealth; } private bool ChekGround() { Collider2D res = Physics2D.OverlapPoint(CheckPoint.position); //���� ����� �� � ��� ��� ������������, �� ����� false if (res != null) { _state = state.Idle; return true; } else //���� ����� ������������ � ������ �����������, �� ����� true { _state = state.Jump; return false; } } public override void Death() { GetComponent<CapsuleCollider2D>().enabled = false; GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeAll; _animator.SetTrigger("Death"); Invoke("ActiveLossPanel", 1f); //��������� ������ ��������� } private void ActiveLossPanel() { LossPanel.SetActive(true); } private void OnCollisionEnter2D(Collision2D collision) { //�������� ������������ � ������. if (collision.gameObject.CompareTag("Enemy")) { collision.gameObject.GetComponent<Character>().GetDamage(1); //����� ������ ��������� ����� ��������� ����������� �����. _rb.AddForce(Vector2.up * JumpPower); } } private void OnTriggerEnter2D(Collider2D collision) { if (collision.CompareTag("Coin")) { _coin += collision.GetComponent<Coin>().PickUp(); CoinText.text = _coin.ToString(); } else if (collision.CompareTag("DeathZone")) { SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); } } }