Advertisement
kadyr

Untitled

Nov 21st, 2021
1,077
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class PlayerController : MonoBehaviour
  7. {
  8.     //переменная здоровья
  9.     private int health;
  10.  
  11.     [SerializeField]
  12.     private Text hpText;
  13.  
  14.    
  15.     //объект пули
  16.     [SerializeField]
  17.     private GameObject bullet;
  18.  
  19.     //позиция появления пули
  20.     [SerializeField]
  21.     private Transform riffleStart;
  22.    
  23.  
  24.  
  25.     private void Update()
  26.     {
  27.         if (Input.GetMouseButtonDown(0))
  28.         {
  29.             var bul = Instantiate(bullet, riffleStart.position, transform.rotation);
  30.             bul.GetComponent<Bullet>().SetDirection(transform.forward);
  31.         }
  32.     }
  33.    
  34.     //метод меняющий здоровье
  35.     public void ChangeHealth(int hp)
  36.     {
  37.         health += hp;
  38.         hpText.text = health.ToString();
  39.     }
  40.  
  41.     private void Start()
  42.     {
  43.         ChangeHealth(100);
  44.     }
  45.  
  46.     private void OnTriggerEnter(Collider other)
  47.     {
  48.         if (other.gameObject.CompareTag("Health"))
  49.         {
  50.             Destroy(other.transform.parent.gameObject);
  51.             ChangeHealth(50);
  52.         }
  53.     }
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement