Harvester AI in Unity3D (C#)

BANANAMAN

Resident Star Battle Expert.
Reaction score
150
Code:
using UnityEngine;

	using System.Collections;

	

	public class HarvestAI : MonoBehaviour {

		

		public GameObject target;

		

		Vector3	newTarget;

		

		public float moveSpeed;

		

		// Use this for initialization

		void Start () {

			target = GetNearestNode();	

			newTarget = target.transform.position;

			

		}

		

		// Update is called once per frame

		void Update () {

			

			transform.LookAt(target.transform.position);

			transform.Translate(0,0,moveSpeed * Time.deltaTime);

			Debug.DrawLine(transform.position, newTarget, Color.red);

		}

		

		void OnTriggerEnter() {

			NodeScript ns = target.GetComponent<NodeScript>();

			target = ns.adjacentNode[0];

			newTarget = target.transform.position;

			

		}

		

		GameObject GetNearestNode(){

			GameObject nearestNode;

			float nearestDistance;

			GameObject[] allTheNodes = GameObject.FindGameObjectsWithTag("Node");

			

			nearestDistance =

				Vector3.Distance(transform.position, allTheNodes[0].transform.position);

			nearestNode = allTheNodes[0];

			

			foreach (GameObject node in allTheNodes) {

				float distanceToTHISNode;

				distanceToTHISNode = 

					Vector3.Distance(transform.position, node.transform.position);

				if (distanceToTHISNode < nearestDistance){

					nearestDistance = distanceToTHISNode;

					nearestNode = node;

				}

			}

			

			return nearestNode;

		}	

	}

This is the AI script I made so that I have a 5 node points placed in a order so as my unit will automatically move from one node to another. The problem is this script only searches for a SINGLE tag (the Node tag) which all the nodes in the pathing map have. I want it so that I have 2 different node types (Resource and "TownHall") where my harvester after switching from Idle state will first go to the Resource and then the TownHall and then go back and forth between them but only will switch from Idle state when there is a resource node near it.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top