Advertisement
kadyr

Untitled

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