Advertisement
kadyr

Untitled

Jul 31st, 2021
1,198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3.  
  4. public class PlayerController : MonoBehaviour
  5. {
  6.     [SerializeField] Text HpText;
  7.     int health;
  8.  
  9.     [SerializeField] GameObject bullet;
  10.     [SerializeField] GameObject rifleStart;
  11.     [SerializeField] GameObject gameOver;
  12.     [SerializeField] GameObject pauseUI;
  13.  
  14.     bool pause = false;
  15.  
  16.     public void ChangeHealth(int count)
  17.     {
  18.         health = health + count;
  19.         if (health <= 0)
  20.         {
  21.             gameOver.SetActive(true);
  22.             GetComponent<PlayerLook>().enabled = false;
  23.             Cursor.lockState = CursorLockMode.None;
  24.         }
  25.         HpText.text = health.ToString();
  26.     }
  27.  
  28.     public int GetHealth()
  29.     {
  30.         return health;
  31.     }
  32.    
  33.     // Start is called before the first frame update
  34.     void Start()
  35.     {
  36.         //+++++++++++++++++++++++
  37.         if (PlayerPrefs.HasKey("playerX"))
  38.         {
  39.             float x = PlayerPrefs.GetFloat("playerX");
  40.             float y = PlayerPrefs.GetFloat("playerY");
  41.             float z = PlayerPrefs.GetFloat("playerZ");
  42.             ChangeHealth(PlayerPrefs.GetInt("health"));
  43.             transform.position = new Vector3(x, y, z);
  44.         }
  45.         else
  46.         {
  47.             ChangeHealth(100);
  48.         }
  49.         //++++++++++++++++++++++++++++
  50.     }
  51.  
  52.     // Update is called once per frame
  53.     void Update()
  54.     {
  55.         if (Input.GetKeyDown(KeyCode.Escape))
  56.         {
  57.             if (pause)
  58.             {
  59.                 pause = false;
  60.                 GetComponent<PlayerLook>().enabled = true;
  61.                 GetComponent<PlayerMove>().enabled = true;
  62.                 Cursor.lockState = CursorLockMode.Locked;
  63.                 pauseUI.SetActive(false);
  64.             }
  65.             else
  66.             {
  67.                 pause = true;
  68.                 GetComponent<PlayerLook>().enabled = false;
  69.                 GetComponent<PlayerMove>().enabled = false;
  70.                 Cursor.lockState = CursorLockMode.None;
  71.                 pauseUI.SetActive(true);
  72.             }
  73.         }
  74.  
  75.         if (Input.GetMouseButtonDown(0) && !pause)
  76.         {
  77.             //создаем пулю
  78.             GameObject buf = Instantiate(bullet);
  79.             buf.transform.position = rifleStart.transform.position;
  80.             buf.transform.rotation = transform.rotation;
  81.             buf.GetComponent<Bullet>().setDirection(transform.forward);
  82.         }
  83.     }
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement