Advertisement
kadyr

Untitled

Aug 28th, 2021
1,105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.83 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI; // Добавление директивы
  6.  
  7. public class PlayerMove : MonoBehaviour
  8. {
  9.     void Start()
  10.     {
  11.         Debug.Log("Press F to pay respect!");
  12.         ChangeHealth(-20);
  13.     }
  14.  
  15.     [SerializeField]
  16.     CharacterController controller;
  17.  
  18.     [SerializeField]
  19.     float speed = 5f;
  20.  
  21.     [SerializeField]
  22.     float gravity = 50;
  23.  
  24.     [SerializeField]
  25.     float jumpForce = 40;
  26.  
  27.     int crystalCount = 0;
  28.     float score = 50f;
  29.  
  30.     int health = 100;
  31.     public Text scoreText; // ТЕКСТ
  32.     public Text timeText; //время нашего таймера (визуализация)
  33.     public Text resultText; // результат = поражение/победа
  34.     public Text hpText;
  35.     private Vector3 direction;
  36.  
  37.     [SerializeField]
  38.     float timeToWin = 10f;
  39.  
  40.     bool lose = false;
  41.  
  42.     public void ChangeHealth(int hp)
  43.     {
  44.         health += hp;
  45.         hpText.text = health.ToString();
  46.         if (health <= 0)
  47.         {
  48.             Cursor.lockState = CursorLockMode.None;
  49.             SceneManager.LoadScene(2);
  50.         }
  51.     }
  52.  
  53.  
  54.     void Update()
  55.     {
  56.         float moveHorizontal = Input.GetAxis("Horizontal");
  57.         float moveVertical = Input.GetAxis("Vertical");
  58.  
  59.         if (controller.isGrounded)
  60.         {
  61.             direction = new Vector3(moveHorizontal, 0, moveVertical);
  62.  
  63.             direction = transform.TransformDirection(direction) * speed;
  64.             if (Input.GetKey(KeyCode.LeftShift))
  65.             {
  66.                 speed = 20f;
  67.             }
  68.  
  69.             if (Input.GetKeyUp(KeyCode.LeftShift))
  70.             {
  71.                 speed = 5f;
  72.             }
  73.  
  74.  
  75.             if (Input.GetKey(KeyCode.Space))
  76.             {
  77.                 direction.y = jumpForce;
  78.             }
  79.         }
  80.  
  81.         //Если будете добавлять что-то связанное с управлением, делайте это здесь
  82.  
  83.         direction.y -= gravity * Time.deltaTime;
  84.         controller.Move(direction * Time.deltaTime);
  85.     }
  86.  
  87.     private void OnTriggerEnter(Collider trigObject)
  88.     {
  89.         if (trigObject.gameObject.tag == "Crystal")
  90.         {
  91.             crystalCount += 1;
  92.             scoreText.text = crystalCount.ToString();
  93.             Destroy(trigObject.gameObject);
  94.             Debug.Log(crystalCount);
  95.             if (crystalCount == 5)
  96.             {
  97.                 resultText.gameObject.SetActive(true);
  98.                 resultText.text = "Ты победил!";
  99.                 Debug.Log("Ты победил!!!");
  100.             }
  101.         }
  102.  
  103.         if (trigObject.gameObject.tag == "Apteka")
  104.         {
  105.             ChangeHealth(30);
  106.             Destroy(trigObject.gameObject);
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement