Advertisement
Guest User

Untitled

a guest
Jun 12th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.25 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class AI : MonoBehaviour
  6. {
  7.     public float axis, speed, timeToRandomizeDir, rotationSpeed, sensor;
  8.     public string direction;
  9.     public int directionRand;
  10.     public bool beingAttracted;
  11.     public GameObject[] food;
  12.     public int foodNumber;
  13.     public float distanceToFood;
  14.  
  15.     private void Start()
  16.     {
  17.         timeToRandomizeDir = Random.Range(0.3f, 3.0f);
  18.     }
  19.     private void Update()
  20.     {
  21.         if (foodNumber > food.Length)
  22.         {
  23.             foodNumber = 0;
  24.         }
  25.         if (distanceToFood > sensor)
  26.         {
  27.             beingAttracted = false;
  28.         } else if (distanceToFood <= sensor & food[foodNumber].GetComponent<Food>().beingHaunted == false)
  29.         {
  30.             beingAttracted = true;
  31.         }
  32.         if (!beingAttracted)
  33.         {
  34.             foodNumber++;
  35.             transform.eulerAngles = new Vector3(0, axis, 0);
  36.             switch (directionRand)
  37.             {
  38.                 case 0:
  39.                     direction = "forward";
  40.                     break;
  41.                 case 1:
  42.                     direction = "right";
  43.                     break;
  44.                 case 2:
  45.                     direction = "left";
  46.                     break;
  47.             }
  48.             timeToRandomizeDir -= Time.deltaTime;
  49.             if (timeToRandomizeDir <= 0)
  50.             {
  51.                 timeToRandomizeDir = Random.Range(0.5f, 3.0f);
  52.                 directionRand = Random.Range(0, 3);
  53.             }
  54.             else if (direction == "left")
  55.             {
  56.                 axis -= Time.deltaTime * rotationSpeed;
  57.             }
  58.             else if (direction == "right")
  59.             {
  60.                 axis += Time.deltaTime * rotationSpeed;
  61.             }
  62.         } else
  63.         {
  64.             transform.LookAt(food[foodNumber].transform);
  65.             if (distanceToFood <= 1.5f)
  66.             {
  67.                 Destroy(food[foodNumber].gameObject);
  68.                 beingAttracted = false;
  69.             }
  70.         }
  71.         this.GetComponent<Rigidbody>().velocity = transform.forward * speed;
  72.         distanceToFood = Vector3.Distance(this.transform.position, food[foodNumber].transform.position);
  73.  
  74.     }
  75.        
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement