Advertisement
kadyr

Untitled

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