Advertisement
kadyr

Untitled

Jul 24th, 2021
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3.  
  4. public class PlayerMove : MonoBehaviour
  5. {
  6.  
  7. public AudioSource walkingSource;
  8. void Start()
  9. {
  10. walkingSource = GetComponent<AudioSource>();
  11. }
  12.  
  13. //Добавляем ссылку на компонент CharacterController в инспектор
  14. [SerializeField] CharacterController controller;
  15. //Добавляем ссылку на переменную speed (скорость игрока) в инспекторе
  16. [SerializeField] float speed = 5f;
  17.  
  18. //Создаем переменную для направления движения игрока
  19. private Vector3 direction;
  20. //Создаем переменную гравитации для падения
  21. float gravity = 20;
  22. float time = 0;
  23. void Update()
  24. {
  25. if (crystals < 5)
  26. {
  27. time += Time.deltaTime;
  28. TimeText.text = time.ToString();
  29. }
  30.  
  31. if (Input.GetKeyDown(KeyCode.LeftShift))
  32. {
  33. speed = 10;
  34. }
  35. if (Input.GetKeyUp(KeyCode.LeftShift))
  36. {
  37. speed = 5;
  38. }
  39.  
  40. // moveHorizontal будет принимать значение -1 если нажата кнопка A, 1 если нажата D, 0 если эти кнопки не нажаты
  41. float moveHorizontal = Input.GetAxis("Horizontal");
  42. // moveVertical будет принимать значение -1 если нажата кнопка S, 1 если нажата W, 0 если эти кнопки не нажаты
  43. float moveVertical = Input.GetAxis("Vertical");
  44.  
  45. if(controller.isGrounded)
  46. {
  47. //Редактируем переменную направления, используя moveHorizontal и moveVertical
  48. //Мы двигаемся по координатам x и z, координата y для прыжков, пока что этого не делаем.
  49. direction = new Vector3(moveHorizontal, 0, moveVertical);
  50. //Дополнительно умножая его на скорость передвижения (преобразуя локальные координаты к глобальным)
  51. direction = transform.TransformDirection(direction) * speed;
  52. if (direction.magnitude > 0)
  53. {
  54. walkingSource.mute = false;
  55. }
  56. else
  57. {
  58. walkingSource.mute = true;
  59. }
  60. //Прыгаем
  61. if (Input.GetKeyDown(KeyCode.Space))
  62. {
  63. direction.y = 20;
  64. }
  65. }
  66.  
  67. direction.y -= gravity * Time.deltaTime;
  68. //Этой строчкой мы осуществляем изменение положения игрока на основе вектора direction
  69. //Time.deltaTime это количество секунд которое прошло с последнего кадра, для синхронизации по времени
  70. controller.Move(direction * Time.deltaTime);
  71. }
  72. [SerializeField] int crystals = 0;
  73. [SerializeField] Text ScoreText;
  74. [SerializeField] Text TimeText;
  75.  
  76. private void OnTriggerEnter(Collider collider)
  77. {
  78. if (collider.tag == "Crystal")
  79. {
  80. print("crystal");
  81. Destroy(collider.gameObject);
  82. crystals = crystals + 1;
  83. ScoreText.text = crystals.ToString();
  84. }
  85. if(collider.tag == "Apteka")
  86. {
  87. GetComponent<PlayerController>().ChangeHealth(20);
  88. Destroy(collider);
  89. }
  90. }
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement