Guest User

Untitled

a guest
Jul 15th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.54 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using UnityEngine.AI;
  7.  
  8. public class enemy : MonoBehaviour
  9. {
  10.  
  11.     public Transform target;
  12.     private Transform player;
  13.     private Rigidbody rb;
  14.     public float spd;
  15.     public bool isHit;
  16.     private float hitStun;
  17.     public float hitTimer = 1;
  18.     public float hp;
  19.     public NavMeshAgent agent;
  20.    
  21.     // Start is called before the first frame update
  22.     void Start()
  23.     {
  24.         agent = GetComponent<NavMeshAgent>();
  25.         rb = GetComponent<Rigidbody>();
  26.        
  27.         player = GameObject.Find("Player").transform;
  28.  
  29.         target = player;
  30.        
  31.  
  32.     }
  33.  
  34.     // Update is called once per frame
  35.     void Update()
  36.     {
  37.        
  38.        
  39.        
  40.         if (isHit && Time.time > hitStun)
  41.         {
  42.             isHit = false;
  43.         }
  44.        
  45.         if (hp <= 0)
  46.         {
  47.             Destroy(gameObject);
  48.         }
  49.        
  50.     }
  51.  
  52.     private void OnTriggerEnter(Collider other)
  53.     {
  54.         if (other.CompareTag("Weapon"))
  55.         {
  56.             hitStun = Time.time + hitTimer;
  57.             isHit = true;
  58.         }
  59.     }
  60.  
  61.     private void FixedUpdate()
  62.     {
  63.        
  64.         if (!isHit)
  65.         {
  66.             agent.enabled = true;
  67.             if (agent.enabled)
  68.             {
  69.                 agent.destination = target.transform.position;
  70.             }
  71.            
  72.         }
  73.        
  74.     }
  75.  
  76.     public void TakeDamage(float dmg)
  77.     {
  78.         hp -= dmg;
  79.     }
  80. }
Add Comment
Please, Sign In to add comment