Advertisement
Guest User

Untitled

a guest
Feb 10th, 2021
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class Run : MonoBehaviour
  6. {
  7. public float Speed = 100f;
  8. public float JumpForce = 4000f;
  9.  
  10. //что бы эта переменная работала добавьте тэг "Ground" на вашу поверхность земли
  11. public bool _isGrounded = false;
  12. private Rigidbody2D _rb;
  13. private Animator animator;
  14. public GameObject PlayerSprite;
  15. float moveHorizontal = 0f;
  16. public GameObject DiePanel;
  17. public GameObject PlayPanel;
  18. public Sprite[] ButtonsController;
  19. public Image[] ImageButtons;
  20.  
  21.  
  22. void Start()
  23. {
  24. PlayPanel.SetActive(true);
  25. DiePanel.SetActive(false);
  26. _rb = GetComponent<Rigidbody2D>();
  27. animator = PlayerSprite.GetComponent<Animator>();
  28. ImageButtons[0] = GetComponent<Image>();
  29. ImageButtons[1] = GetComponent<Image>();
  30. ImageButtons[2] = GetComponent<Image>();
  31. }
  32.  
  33. void FixedUpdate()
  34. {
  35. MovementLogic();
  36. if (!_isGrounded)
  37. {
  38. _rb.AddForce(Vector2.up * -5);
  39. }
  40. }
  41. void OnTriggerStay2D(Collider2D other)
  42. {
  43. if (other.tag == "Enemy" || other.tag == "DeadZone")
  44. {
  45. PlayPanel.SetActive(false);
  46. DiePanel.SetActive(true);
  47. Destroy(this.gameObject);
  48. Time.timeScale = 0;
  49. }
  50.  
  51. }
  52.  
  53. public void Right()
  54. {
  55. PlayerSprite.transform.localRotation = Quaternion.Euler(0, 0, 0);
  56. animator.SetBool("isRuning", true);
  57. moveHorizontal = 1f;
  58. ImageButtons[1].sprite = ButtonsController[1];
  59.  
  60. }
  61. public void OffRight()
  62. {
  63.  
  64. animator.SetBool("isRuning", false);
  65. moveHorizontal = 0f;
  66. ImageButtons[1].sprite = ButtonsController[0];
  67.  
  68. }
  69. public void Left()
  70. {
  71. PlayerSprite.transform.localRotation = Quaternion.Euler(0, 180, 0);
  72. animator.SetBool("isRuning", true);
  73. moveHorizontal = -1f;
  74. ImageButtons[0].sprite = ButtonsController[2];
  75. }
  76. public void OffLeft()
  77. {
  78. animator.SetBool("isRuning", false);
  79. moveHorizontal = 0f;
  80. ImageButtons[0].sprite = ButtonsController[3];
  81. }
  82.  
  83. private void MovementLogic()
  84. {
  85. Vector2 movement = new Vector2(moveHorizontal, 0.0f);
  86. transform.Translate(movement * Speed * Time.fixedDeltaTime);
  87. }
  88.  
  89. public void JumpLogic()
  90. {
  91.  
  92. if (_isGrounded)
  93. {
  94. _rb.AddForce(Vector2.up * JumpForce);
  95.  
  96. }
  97.  
  98. }
  99.  
  100.  
  101.  
  102. }
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement