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 AIController : MonoBehaviour
- {
- public AIController instance;
- public float chaseDis = 5f;
- float timeSinceLastSawPlayer = Mathf.Infinity;
- float waypointArriveTime = Mathf.Infinity;
- float susTime = 3f;
- public float waypointTollerance = 1f;
- public float dwellTime = 3f;
- int currentwaypointIndex = 0;
- public PatrolPath patrolPath;
- Health health;
- Fighter fighter;
- Mover mover;
- public GameObject player;
- Vector3 guardPos;
- private void Start()
- {
- instance = this;
- health = GetComponent<Health>();
- fighter = GetComponent<Fighter>();
- player = GameObject.FindWithTag("Player");
- mover = GetComponent<Mover>();
- guardPos = transform.position;
- }
- private void Update()
- {
- if (health.isDead) return;
- if (InAttackRange())
- {
- AttackBehavior();
- }
- else if (timeSinceLastSawPlayer < susTime)
- {
- SusBehavior();
- }
- else
- {
- PatrolBehavior();
- }
- UpdateTimers();
- }
- private void UpdateTimers()
- {
- timeSinceLastSawPlayer += Time.deltaTime;
- waypointArriveTime += Time.deltaTime;
- }
- private void PatrolBehavior()
- {
- Vector3 nextPos = guardPos;
- if(patrolPath != null)
- {
- if(AtWaypoint())
- {
- waypointArriveTime = 0;
- CycleWaypoint();
- }
- nextPos = GetCurrentWaypoint();
- }
- if(waypointArriveTime > dwellTime)
- {
- mover.StartMoveAction(nextPos);
- }
- }
- private void AttackBehavior()
- {
- timeSinceLastSawPlayer = 0;
- fighter.Attack(player);
- }
- private void SusBehavior()
- {
- }
- private bool AtWaypoint()
- {
- float disToWaypoint = Vector3.Distance(transform.position, GetCurrentWaypoint());
- return disToWaypoint < waypointTollerance;
- }
- private Vector3 GetCurrentWaypoint()
- {
- return patrolPath.GetWayPoint(currentwaypointIndex);
- }
- private void CycleWaypoint()
- {
- currentwaypointIndex = patrolPath.GetNextIndex(currentwaypointIndex);
- }
- private bool InAttackRange()
- {
- float distoPlayer = Vector3.Distance(player.transform.position, transform.position);
- return distoPlayer < chaseDis;
- }
- private void OnDrawGizmosSelected()
- {
- Gizmos.color = Color.red;
- Gizmos.DrawWireSphere(transform.position, chaseDis);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement