Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.AI;
- public class MonsterController : MonoBehaviour
- {
- // Start is called before the first frame update
- public GameObject target;
- private NavMeshAgent bot;
- public int damage = 10;
- public int hp = 100;
- private float name_margin = 1f;
- private Color startColor;
- Renderer render;
- void Start()
- {
- target = GameObject.FindGameObjectWithTag("Base");
- bot = GetComponent<NavMeshAgent>();
- bot.destination = target.transform.position;
- render = gameObject.GetComponent<Renderer>();
- startColor = render.material.color;
- }
- private void Update()
- {
- if(hp <= 0)
- {
- Destroy(gameObject);
- }
- }
- void OnTriggerEnter(Collider col)
- {
- Debug.Log(col.gameObject);
- if(col.gameObject.tag == "Base")
- {
- GameSystem.HP -= damage;
- Destroy(gameObject);
- }
- }
- private void OnGUI()
- {
- Vector3 pos = new Vector3(transform.position.x - name_margin,
- transform.position.y,
- transform.position.z);
- Vector3 crd = Camera.main.WorldToScreenPoint(pos);
- crd.y = Screen.height - crd.y;
- GUIStyle style = new GUIStyle();
- style.fontSize = 12;
- style.normal.textColor = Color.red;
- style.alignment = TextAnchor.MiddleCenter;
- float hpPercent = (float)hp/100 * 100;
- GUI.Label(new Rect(
- crd.x - 50, crd.y, 100, 20),
- $"Monster {hpPercent}%", style);
- }
- public void ReturnColor()
- {
- render.material.color = startColor;
- }
- public void AttackIndicate()
- {
- render.material.color = Color.red;
- Invoke("ReturnColor", 0.5f);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement