Advertisement
Guest User

Untitled

a guest
Mar 6th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.17 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class BotAI : MonoBehaviour
  5. {
  6.     float AIspeed = 3.5f;
  7.     float MaxDistance = 50f;
  8.     float MinDistance = 1f;
  9.     float AIrotate = 90f;
  10.  
  11.     Transform Leader;
  12.     bool AImoving = true;
  13.  
  14.     void Start()
  15.     {
  16.         Leader = GameObject.FindWithTag(Tags.Player).transform;
  17.     }
  18.  
  19.     void Update()
  20.     {
  21.         float distance = Vector3.Distance(transform.position, Leader.position);
  22.  
  23.         if (distance == MinDistance)
  24.             transform.LookAt(Leader);
  25.         else if (MinDistance < distance && distance <= MaxDistance)
  26.         {
  27.             transform.position += transform.forward * AIspeed * Time.deltaTime;
  28.             transform.LookAt(Leader);
  29.         }
  30.         else
  31.             StartCoroutine(Patrol());
  32.  
  33.     }
  34.  
  35.     IEnumerator Patrol()
  36.     {
  37.         if (AImoving)
  38.         {
  39.             transform.position += transform.forward * AIspeed * Time.deltaTime;
  40.             yield return new WaitForSeconds(1.5f);
  41.             AImoving = false;
  42.         }
  43.         transform.Rotate(0, AIrotate * Time.deltaTime, 0);
  44.         yield return new WaitForSeconds(1.5f);
  45.         AImoving = true;
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement