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 AI : MonoBehaviour
- {
- public float axis, speed, timeToRandomizeDir, rotationSpeed, sensor;
- public string direction;
- public int directionRand;
- public bool beingAttracted;
- public GameObject[] food;
- public int foodNumber;
- public float distanceToFood;
- private void Start()
- {
- timeToRandomizeDir = Random.Range(0.3f, 3.0f);
- }
- private void Update()
- {
- if (foodNumber > food.Length)
- {
- foodNumber = 0;
- }
- if (distanceToFood > sensor)
- {
- beingAttracted = false;
- } else if (distanceToFood <= sensor & food[foodNumber].GetComponent<Food>().beingHaunted == false)
- {
- beingAttracted = true;
- }
- if (!beingAttracted)
- {
- foodNumber++;
- transform.eulerAngles = new Vector3(0, axis, 0);
- switch (directionRand)
- {
- case 0:
- direction = "forward";
- break;
- case 1:
- direction = "right";
- break;
- case 2:
- direction = "left";
- break;
- }
- timeToRandomizeDir -= Time.deltaTime;
- if (timeToRandomizeDir <= 0)
- {
- timeToRandomizeDir = Random.Range(0.5f, 3.0f);
- directionRand = Random.Range(0, 3);
- }
- else if (direction == "left")
- {
- axis -= Time.deltaTime * rotationSpeed;
- }
- else if (direction == "right")
- {
- axis += Time.deltaTime * rotationSpeed;
- }
- } else
- {
- transform.LookAt(food[foodNumber].transform);
- if (distanceToFood <= 1.5f)
- {
- Destroy(food[foodNumber].gameObject);
- beingAttracted = false;
- }
- }
- this.GetComponent<Rigidbody>().velocity = transform.forward * speed;
- distanceToFood = Vector3.Distance(this.transform.position, food[foodNumber].transform.position);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement