Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class Run : MonoBehaviour
- {
- public float Speed = 100f;
- public float JumpForce = 4000f;
- //что бы эта переменная работала добавьте тэг "Ground" на вашу поверхность земли
- public bool _isGrounded = false;
- private Rigidbody2D _rb;
- private Animator animator;
- public GameObject PlayerSprite;
- float moveHorizontal = 0f;
- public GameObject DiePanel;
- public GameObject PlayPanel;
- public Sprite[] ButtonsController;
- public Image[] ImageButtons;
- void Start()
- {
- PlayPanel.SetActive(true);
- DiePanel.SetActive(false);
- _rb = GetComponent<Rigidbody2D>();
- animator = PlayerSprite.GetComponent<Animator>();
- ImageButtons[0] = GetComponent<Image>();
- ImageButtons[1] = GetComponent<Image>();
- ImageButtons[2] = GetComponent<Image>();
- }
- void FixedUpdate()
- {
- MovementLogic();
- if (!_isGrounded)
- {
- _rb.AddForce(Vector2.up * -5);
- }
- }
- void OnTriggerStay2D(Collider2D other)
- {
- if (other.tag == "Enemy" || other.tag == "DeadZone")
- {
- PlayPanel.SetActive(false);
- DiePanel.SetActive(true);
- Destroy(this.gameObject);
- Time.timeScale = 0;
- }
- }
- public void Right()
- {
- PlayerSprite.transform.localRotation = Quaternion.Euler(0, 0, 0);
- animator.SetBool("isRuning", true);
- moveHorizontal = 1f;
- ImageButtons[1].sprite = ButtonsController[1];
- }
- public void OffRight()
- {
- animator.SetBool("isRuning", false);
- moveHorizontal = 0f;
- ImageButtons[1].sprite = ButtonsController[0];
- }
- public void Left()
- {
- PlayerSprite.transform.localRotation = Quaternion.Euler(0, 180, 0);
- animator.SetBool("isRuning", true);
- moveHorizontal = -1f;
- ImageButtons[0].sprite = ButtonsController[2];
- }
- public void OffLeft()
- {
- animator.SetBool("isRuning", false);
- moveHorizontal = 0f;
- ImageButtons[0].sprite = ButtonsController[3];
- }
- private void MovementLogic()
- {
- Vector2 movement = new Vector2(moveHorizontal, 0.0f);
- transform.Translate(movement * Speed * Time.fixedDeltaTime);
- }
- public void JumpLogic()
- {
- if (_isGrounded)
- {
- _rb.AddForce(Vector2.up * JumpForce);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement