Advertisement
Guest User

Troll Fix

a guest
Apr 6th, 2020
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class TrolAI_II : MonoBehaviour
  6. {
  7.  
  8.     // Public variables
  9.     public GameObject           player;
  10.     public Animator             anim; // Animator to animate character behavior
  11.     public List<GameObject>     waypoints = new List<GameObject>();
  12.     public List<GameObject>     TrollEnemies = new List<GameObject>();
  13.     public float                attackRange = 2.0f;
  14.     public float                moveSpeed = 1.0f;
  15.     public float                followSpeed = 1.0f;
  16.  
  17.     // Private variables
  18.     GameObject TrolEnemy = null;
  19.     int num = 0;
  20.  
  21.  
  22.     void Start()  
  23.     {  
  24.     }
  25.  
  26.     void Update()
  27.     {
  28.         if (TrollEnemies.Capacity > 0)
  29.         {
  30.             // If there are any enemies on the list, follow the first one
  31.             anim.SetBool("Walk", true);
  32.             Debug.Log("Following enemy");
  33.             float enemyDistance = Vector3.Distance(TrollEnemies[0].gameObject.transform.position, transform.position);
  34.             transform.LookAt((TrollEnemies[0].gameObject.transform.position));
  35.             transform.position += transform.forward * followSpeed * Time.deltaTime;
  36.            
  37.             // If enemy is in attack range, attack it!
  38.             if (enemyDistance < attackRange)
  39.             {
  40.                 Debug.Log( "Attacking enemy!" );
  41.                 anim.SetBool("Attack", true);  
  42.  
  43.                 // This might not be the best way to do this because it will be called every frame!
  44.                 // It's good to remember that you are attacking and do nothing until attack ends, then you resume
  45.                 // with following the enemy or attacking it.
  46.             }
  47.  
  48.         } else {
  49.             // No enemies on the list, just walk around the waypoints...
  50.             Patrol();
  51.         }
  52.     }
  53.            
  54.    
  55.     // Link to this method from attack animation so you know when the HIT happens / when the attack ends
  56.     public void OnAttack()
  57.     {
  58.     }
  59.  
  60.    
  61.     private void Patrol()
  62.     {
  63.         float dist = Vector3.Distance(transform.position, waypoints[num].transform.position);
  64.         if (dist > 1)
  65.         {
  66.             anim.SetBool("Walk", true);
  67.             transform.LookAt(waypoints[num].transform.position);
  68.             transform.position += transform.forward * moveSpeed * Time.deltaTime;
  69.         }
  70.         else
  71.         {
  72.             num++;
  73.             if( num >= waypoints.Capacity ) num = 0;
  74.         }
  75.  
  76.  
  77.     }
  78.     private void OnTriggerEnter(Collider other)
  79.     {
  80.         if (other.tag == "Player")
  81.         {
  82.  
  83.             TrollEnemies.Add(other.gameObject);
  84.             Debug.Log("aghhhhhh!!!!");
  85.         }
  86.     }
  87.  
  88.     private void OnTriggerExit(Collider other)
  89.     {
  90.         TrollEnemies.Remove(GameObject.Find(other.name));
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement