Advertisement
sphinx2001

MonsterController.cs

Feb 2nd, 2021
784
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.89 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5.  
  6. public class MonsterController : MonoBehaviour
  7. {
  8.     // Start is called before the first frame update
  9.     public GameObject target;
  10.     private NavMeshAgent bot;
  11.     public int damage = 10;
  12.     public int hp = 100;
  13.     private float name_margin = 1f;
  14.     private Color startColor;
  15.     Renderer render;
  16.     void Start()
  17.     {
  18.         target = GameObject.FindGameObjectWithTag("Base");
  19.  
  20.         bot = GetComponent<NavMeshAgent>();
  21.         bot.destination = target.transform.position;
  22.         render = gameObject.GetComponent<Renderer>();        
  23.         startColor = render.material.color;
  24.     }
  25.     private void Update()
  26.     {
  27.         if(hp <= 0)
  28.         {
  29.             Destroy(gameObject);
  30.         }
  31.     }
  32.     void OnTriggerEnter(Collider col)
  33.     {
  34.         Debug.Log(col.gameObject);
  35.  
  36.         if(col.gameObject.tag == "Base")
  37.         {
  38.             GameSystem.HP -= damage;
  39.             Destroy(gameObject);
  40.         }
  41.     }
  42.  
  43.     private void OnGUI()
  44.     {
  45.         Vector3 pos = new Vector3(transform.position.x - name_margin,
  46.             transform.position.y,
  47.             transform.position.z);
  48.         Vector3 crd = Camera.main.WorldToScreenPoint(pos);
  49.         crd.y = Screen.height - crd.y;
  50.  
  51.         GUIStyle style = new GUIStyle();
  52.         style.fontSize = 12;
  53.         style.normal.textColor = Color.red;
  54.         style.alignment = TextAnchor.MiddleCenter;
  55.         float hpPercent = (float)hp/100 * 100;
  56.         GUI.Label(new Rect(
  57.             crd.x - 50, crd.y, 100, 20),
  58.             $"Monster {hpPercent}%", style);
  59.     }
  60.    
  61.     public void ReturnColor()
  62.     {
  63.         render.material.color = startColor;
  64.     }
  65.     public void AttackIndicate()
  66.     {
  67.        
  68.         render.material.color = Color.red;
  69.         Invoke("ReturnColor", 0.5f);
  70.        
  71.     }
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement