Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class TrolAI_II : MonoBehaviour
- {
- // Public variables
- public GameObject player;
- public Animator anim; // Animator to animate character behavior
- public List<GameObject> waypoints = new List<GameObject>();
- public List<GameObject> TrollEnemies = new List<GameObject>();
- public float attackRange = 2.0f;
- public float moveSpeed = 1.0f;
- public float followSpeed = 1.0f;
- // Private variables
- GameObject TrolEnemy = null;
- int num = 0;
- void Start()
- {
- }
- void Update()
- {
- if (TrollEnemies.Capacity > 0)
- {
- // If there are any enemies on the list, follow the first one
- anim.SetBool("Walk", true);
- Debug.Log("Following enemy");
- float enemyDistance = Vector3.Distance(TrollEnemies[0].gameObject.transform.position, transform.position);
- transform.LookAt((TrollEnemies[0].gameObject.transform.position));
- transform.position += transform.forward * followSpeed * Time.deltaTime;
- // If enemy is in attack range, attack it!
- if (enemyDistance < attackRange)
- {
- Debug.Log( "Attacking enemy!" );
- anim.SetBool("Attack", true);
- // This might not be the best way to do this because it will be called every frame!
- // It's good to remember that you are attacking and do nothing until attack ends, then you resume
- // with following the enemy or attacking it.
- }
- } else {
- // No enemies on the list, just walk around the waypoints...
- Patrol();
- }
- }
- // Link to this method from attack animation so you know when the HIT happens / when the attack ends
- public void OnAttack()
- {
- }
- private void Patrol()
- {
- float dist = Vector3.Distance(transform.position, waypoints[num].transform.position);
- if (dist > 1)
- {
- anim.SetBool("Walk", true);
- transform.LookAt(waypoints[num].transform.position);
- transform.position += transform.forward * moveSpeed * Time.deltaTime;
- }
- else
- {
- num++;
- if( num >= waypoints.Capacity ) num = 0;
- }
- }
- private void OnTriggerEnter(Collider other)
- {
- if (other.tag == "Player")
- {
- TrollEnemies.Add(other.gameObject);
- Debug.Log("aghhhhhh!!!!");
- }
- }
- private void OnTriggerExit(Collider other)
- {
- TrollEnemies.Remove(GameObject.Find(other.name));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement