Guest User

Untitled

a guest
Dec 12th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.99 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class AI : MonoBehaviour
  5. {
  6.     public GameObject player;
  7.     public GameObject spawnPoint;
  8.  
  9.     public float teleportDelay;
  10.  
  11.     [SerializeField]
  12.     public float chanceOfTeleporting;
  13.     [SerializeField]
  14.     public float chanceOfDisappearing;
  15.  
  16.     private bool teleporting;
  17.     public float teleportDistance;
  18.  
  19.  
  20.     public void Start()
  21.     {
  22.         player = GameObject.FindGameObjectWithTag("Player");
  23.     }
  24.  
  25.     public void OnBecameVisible()
  26.     {
  27.         ChanceTeleport();
  28.         StartCoroutine("Disappear");
  29.     }
  30.  
  31.  
  32.     public void Update ()
  33.     {
  34.         if (Vector3.Distance(transform.position, player.transform.position) <= teleportDistance)
  35.         {
  36.             teleporting = false;
  37.         }
  38.  
  39.         if (teleporting)
  40.         {
  41.             Teleport();
  42.         }
  43.     }
  44.  
  45.  
  46.     public void ChanceTeleport()
  47.     {
  48.         float randomValue = Random.value;
  49.         teleportDistance = Random.Range(1.0F, 20.0F);
  50.  
  51.         chanceOfTeleporting += 0.05F;
  52.  
  53.         if (randomValue <= chanceOfTeleporting)
  54.         {
  55.             teleporting = true;
  56.         }
  57.     }
  58.  
  59.     public void ChanceDisappear()
  60.     {
  61.         float randomValue = Random.value;
  62.  
  63.         chanceOfTeleporting += 0.3F;
  64.  
  65.         if (randomValue <= chanceOfDisappearing)
  66.         {
  67.             Disappear();
  68.         }
  69.     }
  70.  
  71.  
  72.  
  73.     public void Teleport()
  74.     {
  75.         if (transform.position != player.transform.position)
  76.         {
  77.             transform.position = Vector3.Lerp(transform.position, player.transform.position, Time.deltaTime * 10);
  78.         }
  79.         else
  80.         {
  81.             teleporting = false;
  82.         }
  83.     }
  84.  
  85.     public IEnumerator Disappear()
  86.     {
  87.         if (teleporting == false)
  88.         {
  89.             StopCoroutine("Disappear");
  90.         }
  91.         else
  92.         {
  93.             yield return new WaitForSeconds(teleportDelay);
  94.             transform.position = spawnPoint.transform.position;
  95.         }
  96.     }
  97. }
Add Comment
Please, Sign In to add comment