Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class AI : MonoBehaviour
- {
- public GameObject player;
- public GameObject spawnPoint;
- public float teleportDelay;
- [SerializeField]
- public float chanceOfTeleporting;
- [SerializeField]
- public float chanceOfDisappearing;
- private bool teleporting;
- public float teleportDistance;
- public void Start()
- {
- player = GameObject.FindGameObjectWithTag("Player");
- }
- public void OnBecameVisible()
- {
- ChanceTeleport();
- StartCoroutine("Disappear");
- }
- public void Update ()
- {
- if (Vector3.Distance(transform.position, player.transform.position) <= teleportDistance)
- {
- teleporting = false;
- }
- if (teleporting)
- {
- Teleport();
- }
- }
- public void ChanceTeleport()
- {
- float randomValue = Random.value;
- teleportDistance = Random.Range(1.0F, 20.0F);
- chanceOfTeleporting += 0.05F;
- if (randomValue <= chanceOfTeleporting)
- {
- teleporting = true;
- }
- }
- public void ChanceDisappear()
- {
- float randomValue = Random.value;
- chanceOfTeleporting += 0.3F;
- if (randomValue <= chanceOfDisappearing)
- {
- Disappear();
- }
- }
- public void Teleport()
- {
- if (transform.position != player.transform.position)
- {
- transform.position = Vector3.Lerp(transform.position, player.transform.position, Time.deltaTime * 10);
- }
- else
- {
- teleporting = false;
- }
- }
- public IEnumerator Disappear()
- {
- if (teleporting == false)
- {
- StopCoroutine("Disappear");
- }
- else
- {
- yield return new WaitForSeconds(teleportDelay);
- transform.position = spawnPoint.transform.position;
- }
- }
- }
Add Comment
Please, Sign In to add comment