Advertisement
kadyr

Untitled

Oct 10th, 2021
240
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.  
  23. private float timer;
  24. public Text timeText;
  25.  
  26. void Update()
  27. {
  28. if (crystals < 5)
  29. {
  30. timer += Time.deltaTime;
  31. timeText.text = ((int)timer).ToString();
  32. }
  33. //Ускоряемся
  34. if (Input.GetKeyDown(KeyCode.LeftShift))
  35. {
  36. speed = 10;
  37. }
  38. if (Input.GetKeyUp(KeyCode.LeftShift))
  39. {
  40. speed = 5;
  41. }
  42.  
  43. // moveHorizontal будет принимать значение -1 если нажата кнопка A, 1 если нажата D, 0 если эти кнопки не нажаты
  44. float moveHorizontal = Input.GetAxis("Horizontal");
  45. // moveVertical будет принимать значение -1 если нажата кнопка S, 1 если нажата W, 0 если эти кнопки не нажаты
  46. float moveVertical = Input.GetAxis("Vertical");
  47.  
  48. if(controller.isGrounded)
  49. {
  50. //Редактируем переменную направления, используя moveHorizontal и moveVertical
  51. //Мы двигаемся по координатам x и z, координата y для прыжков, пока что этого не делаем.
  52. direction = new Vector3(moveHorizontal, 0, moveVertical);
  53. //Дополнительно умножая его на скорость передвижения (преобразуя локальные координаты к глобальным)
  54. direction = transform.TransformDirection(direction) * speed;
  55.  
  56. //Прыгаем
  57. if (Input.GetKeyDown(KeyCode.Space))
  58. {
  59. direction.y = 8;
  60. }
  61. }
  62.  
  63. direction.y -= gravity * Time.deltaTime;
  64. //Этой строчкой мы осуществляем изменение положения игрока на основе вектора direction
  65. //Time.deltaTime это количество секунд которое прошло с последнего кадра, для синхронизации по времени
  66. controller.Move(direction * Time.deltaTime);
  67. }
  68. [SerializeField] int crystals = 0;
  69.  
  70. private void OnTriggerEnter(Collider collider)
  71. {
  72. if (collider.tag == "Crystal")
  73. {
  74. print("crystal");
  75. Destroy(collider.gameObject);
  76. crystals = crystals + 1;
  77. scoreText.text = crystals.ToString();
  78. }
  79. }
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement